// compression/GZIPcompress.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.// {java GZIPcompress GZIPcompress.java}// {VisuallyInspectOutput}publicclassGZIPcompress{publicstaticvoidmain(String[]args){if(args.length==0){System.out.println("Usage: \nGZIPcompress file\n"+"\tUses GZIP compression to compress "+"the file to test.gz");System.exit(1);}try(InputStream in =newBufferedInputStream(new FileInputStream(args[0]));BufferedOutputStream out =newBufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")))){System.out.println("Writing file");int c;while((c =in.read())!=-1)out.write(c);}catch(IOExceptione){thrownewRuntimeException(e);}System.out.println("Reading file");try(BufferedReader in2 =newBufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))))){in2.lines().forEach(System.out::println);}catch(IOExceptione){thrownewRuntimeException(e);}}}
支持 Zip 格式的库比 GZIP 库更广泛。有了它,你可以轻松存储多个文件,甚至还有一个单独的类可以轻松地读取 Zip 文件。该库使用标准 Zip 格式,因此它可以与当前可在 Internet 上下载的所有 Zip 工具无缝协作。以下示例与前一个示例具有相同的形式,但它可以根据需要处理任意数量的命令行参数。此外,它还显示了 Checksum 类计算和验证文件的校验和。有两种校验和类型:Adler32(更快)和 CRC32(更慢但更准确)。
对于要添加到存档的每个文件,必须调用 putNextEntry() 并传递 ZipEntry 对象。 ZipEntry 对象包含一个扩展接口,用于获取和设置 Zip 文件中该特定条目的所有可用数据:名称,压缩和未压缩大小,日期,CRC 校验和,额外字段数据,注释,压缩方法以及它是否是目录条目。但是,即使 Zip 格式有设置密码的方法,Java 的 Zip 库也不支持。虽然 CheckedInputStream 和 CheckedOutputStream 都支持 Adler32 和 CRC32 校验和,但 ZipEntry 类仅支持 CRC 接口。这是对基础 Zip 格式的限制,但它可能会限制你使用更快的 Adler32。
// compression/ZipCompress.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// Uses Zip compression to compress any
// number of files given on the command line
// {java ZipCompress ZipCompress.java}
// {VisuallyInspectOutput}
public class ZipCompress {
public static void main(String[] args) {
try (
FileOutputStream f =
new FileOutputStream("test.zip");
CheckedOutputStream csum =
new CheckedOutputStream(f, new Adler32());
ZipOutputStream zos = new ZipOutputStream(csum);
BufferedOutputStream out =
new BufferedOutputStream(zos)
) {
zos.setComment("A test of Java Zipping");
// No corresponding getComment(), though.
for (String arg : args) {
System.out.println("Writing file " + arg);
try (
InputStream in = new BufferedInputStream(
new FileInputStream(arg))
) {
zos.putNextEntry(new ZipEntry(arg));
int c;
while ((c = in.read()) != -1)
out.write(c);
}
out.flush();
}
// Checksum valid only after the file is closed!
System.out.println(
"Checksum: " + csum.getChecksum().getValue());
} catch (IOException e) {
throw new RuntimeException(e);
}
// Now extract the files:
System.out.println("Reading file");
try (
FileInputStream fi =
new FileInputStream("test.zip");
CheckedInputStream csumi =
new CheckedInputStream(fi, new Adler32());
ZipInputStream in2 = new ZipInputStream(csumi);
BufferedInputStream bis =
new BufferedInputStream(in2)
) {
ZipEntry ze;
while ((ze = in2.getNextEntry()) != null) {
System.out.println("Reading file " + ze);
int x;
while ((x = bis.read()) != -1)
System.out.write(x);
}
if (args.length == 1)
System.out.println(
"Checksum: " + csumi.getChecksum().getValue());
} catch (IOException e) {
throw new RuntimeException(e);
}
// Alternative way to open and read Zip files:
try (
ZipFile zf = new ZipFile("test.zip")
) {
Enumeration e = zf.entries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
System.out.println("File: " + ze2);
// ... and extract the data as before
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}