javazip压缩级别_java.util.zip.ZipOutputStream.set。。。java.util.zip.ZipOutputStream.setLevel(int level)⽅法为后续的DEFLATED条⽬设置压缩级别。 默认设置是
DEFAULT_COMPRESSION。
声明
以下是java.util.zip.ZipOutputStream.setLevel(int level)⽅法的声明。
public void setLevel(int level)
参数
level - 压缩级别(0-9)。
异常
IllegalArgumentException - 如果指定的ZIP⽂件注释的长度⼤于0xFFFF字节。
前提条件
在D:/test>⽬录下⽤以下内容创建⼀个⽂件。
This is an example.
⽰例
以下⽰例显⽰了java.util.zip.ZipOutputStream.setLevel(int level)⽅法的⽤法。
package com.yiibai;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.Deflater;
public class ZipOutputStreamDemo {undefined
private static String SOURCE_FILE = "D:\\test\\";
private static String TARGET_FILE = "D:\\test\\Hello.zip";
public static void main(String[] args) {undefined
try {undefined
createZipFile();
readZipFile();
} catch(IOException ioe) {undefined
System.out.println("IOException : " + ioe);
}
}
private static void createZipFile() throws IOException{undefined
FileOutputStream fout = new FileOutputStream(TARGET_FILE); CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32()); ZipOutputStream zout = new ZipOutputStream(checksum);
zout.setLevel(Deflater.DEFAULT_COMPRESSION);
FileInputStream fin = new FileInputStream(SOURCE_FILE);
ZipEntry zipEntry = new ZipEntry(SOURCE_FILE);
zout.putNextEntry(zipEntry);
int length;
byte[] buffer = new byte[1024];
while((length = ad(buffer)) > 0) {undefined
zout.write(buffer, 0, length);
}
zout.closeEntry();
zout.finish();
fin.close();
zout.close();
}
private static void readZipFile() throws IOException{undefined
ZipInputStream zin = new ZipInputStream(new FileInputStream(TARGET_FILE)); ZipEntry entry;
while((entry = NextEntry())!=null){undefined
System.out.printf("File: %s Modified on %TD %n",
extractFile(entry, zin);
System.out.printf("Zip file %s extracted successfully.", SOURCE_FILE);
zin.closeEntry();
}
zin.close();
}
private static void extractFile(final ZipEntry entry, ZipInputStream is)java stream
throws IOException {undefined
FileOutputStream fos = null;
try {undefined
fos = new Name());
while(is.available() != 0){undefined
fos.ad());
}
} catch (IOException ioex) {undefined
fos.close();
}
}
}
执⾏上⾯⽰例代码,得到以下结果 -
Zip File: D:\test\Hello.zip, Contains 1 file(s).
File: D:\ Size 1026 Modified on 05/22/17
Zip file D:\ extracted successfully.
¥ 我要打赏
纠错/补充
收藏
加QQ啦,易百教程官⽅技术学习
注意:建议每个⼈选⾃⼰的技术⽅向加,同⼀个QQ最多限加 3 个。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论