解释
使用不同的方法:
流动
文件。
也就是说,不要将其完全加载到内存中,而是逐字节读取它,同时逐字节写回它。
获取
InputStream
到文件中,包装一些
GZipInputStream
围绕读取每个字节的字节,写入
OutputStream
。
如果要压缩存档,则相反。然后你把
输出流
一些人
GZipOutputStream
。
密码
示例使用
Apache Commons Compress
但所有库的代码逻辑都保持不变。
打开包装a
gz
存档:
Path inputPath = Paths.get("archive.tar.gz");
Path outputPath = Paths.get("archive.tar");
try (InputStream fin = Files.newInputStream(inputPath );
OutputStream out = Files.newOutputStream(outputPath);) {
GZipCompressorInputStream in = new GZipCompressorInputStream(
new BufferedInputStream(fin));
// Read and write byte by byte
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
}
包装组件
广州
存档:
Path inputPath = Paths.get("archive.tar");
Path outputPath = Paths.get("archive.tar.gz");
try (InputStream in = Files.newInputStream(inputPath);
OutputStream fout = Files.newOutputStream(outputPath);) {
GZipCompressorOutputStream out = new GZipCompressorOutputStream(
new BufferedOutputStream(fout));
// Read and write byte by byte
final byte[] buffer = new byte[buffersize];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
}
您也可以包装
BufferedReader
和
PrintWriter
如果你觉得和他们在一起更舒服的话。它们自己管理缓冲区,您可以读写
line
s而不是
byte
s、 请注意,只有当您读取的文件中包含行而不是其他格式时,这才是正确的。