Appendix-Data-Compression
附录: 数据压缩
这些类不是从
压缩类 | 功能 |
---|---|
CheckedInputStream | getCheckSum() 可以对任意 |
CheckedOutputStream | getCheckSum() 可以对任意 |
DeflaterOutputStream | 压缩类的基类 |
ZipOutputStream | |
GZIPOutputStream | |
InflaterInputStream | 解压类的基类 |
ZipInputStream | |
GZIPInputStream |
尽管存在很多压缩算法,但是
使用Gzip 简单压缩
// 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}
public class GZIPcompress {
public static void main(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 = new BufferedInputStream(
new FileInputStream(args[0]));
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz")))
) {
System.out.println("Writing file");
int c;
while ((c = in.read()) != -1)
out.write(c);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("Reading file");
try (
BufferedReader in2 = new BufferedReader(
new InputStreamReader(new GZIPInputStream(
new FileInputStream("test.gz"))))
) {
in2.lines().forEach(System.out::println);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
使用压缩类非常简单,你只需要把你的输出流包装在
使用zip 多文件存储
支持
// 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);
}
}
}
对于要添加到存档的每个文件,必须调用 putNextEntry()
并传递
要提取文件,getNextEntry()
方法,这个方法在有文件存在的情况下调用,会返回下一个
要读取校验和,你必须以某种方式访问关联的setComment()
。如
使用
Java 的jar
jar [options] destination [manifest] inputfile(s)
选项是一组字母(不需要连字符或任何其他指示符
选项 | 功能 |
---|---|
c | 创建一个新的或者空的归档文件 |
t | 列出内容目录 |
x | 提取所有文件 |
x file | 提取指定的文件 |
f | 这代表着 |
m | 代表第一个参数是用户创建的清单文件的名称。 |
v | 生成详细的输出用于表述 |
0 | 仅存储文件 |
M | 不要自动创建清单文件 |
如果放入
以下是一些调用
jar cf myJarFile.jar *.class
下一个命令与前面的示例类似,但它添加了一个名为
jar cmf myJarFile.jar myManifestFile.mf *.class
这个命令输出了
jar tf myJarFile.jar
如下添加了一个“verbose”的标志,用于生成更多关于
jar tvf myJarFile.jar
假设
jar cvf myApp.jar audio classes image
如果你在创建
CLASSPATH="lib1.jar;lib2.jar;"
然后
此外,你无法将文件移动到
但是,在一个平台上创建的