apache commons 压缩的 Tar 问题
我很难尝试使用压缩库来压缩某些文件。
我的代码如下,取自 commons.compress wiki 示例:
private static File createTarFile(String[] filePaths, String saveAs) throws Exception{
File tarFile = new File(saveAs);
OutputStream out = new FileOutputStream(tarFile);
TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
for(String filePath : filePaths){
File file = new File(filePath);
TarArchiveEntry entry = new TarArchiveEntry(file);
entry.setSize(file.length());
aos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(file), aos);
aos.closeArchiveEntry();
}
aos.finish();
out.close();
return tarFile;
}
过程中没有错误,但是当我尝试解压文件时,我得到以下信息:
XXXX:XXXX /home/XXXX$ tar -xf typeCommandes.tar
tar: Unexpected EOF in archive
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
另外,存档的大小比原始文件,这对于 tar 来说是不正常的,所以确实有问题......
-rw-r--r-- 1 XXXX nobody 12902400 Jan 14 17:11 typeCommandes.tar
-rw-r--r-- 1 XXXX nobody 12901888 Jan 14 17:16 typeCommandes.csv
任何人都可以告诉我我做错了什么?谢谢
I'm having a hard time trying to tar some files using the compress library.
My code is the following, and is taken from the commons.compress wiki exemples :
private static File createTarFile(String[] filePaths, String saveAs) throws Exception{
File tarFile = new File(saveAs);
OutputStream out = new FileOutputStream(tarFile);
TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", out);
for(String filePath : filePaths){
File file = new File(filePath);
TarArchiveEntry entry = new TarArchiveEntry(file);
entry.setSize(file.length());
aos.putArchiveEntry(entry);
IOUtils.copy(new FileInputStream(file), aos);
aos.closeArchiveEntry();
}
aos.finish();
out.close();
return tarFile;
}
There is no error during the process, but when I try to untar the file, I got the following :
XXXX:XXXX /home/XXXX$ tar -xf typeCommandes.tar
tar: Unexpected EOF in archive
tar: Unexpected EOF in archive
tar: Error is not recoverable: exiting now
Also, the archive IS slighty smaller in size than the original file, which isnt normal for a tar, so there DO is a problem...
-rw-r--r-- 1 XXXX nobody 12902400 Jan 14 17:11 typeCommandes.tar
-rw-r--r-- 1 XXXX nobody 12901888 Jan 14 17:16 typeCommandes.csv
Anyone can tell me what I'm doing wrong ? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有关闭
TarArchiveOutputStream
。在aos.finish()
之后添加aos.close()
You're not closing the
TarArchiveOutputStream
. Addaos.close()
afteraos.finish()
对上面的代码进行小修正。
它不会关闭输入流,而 Apache lib 假定流是通过调用 client 来管理的。
请参阅下面的修复(将此代码放在“aos.putArchiveEntry(entry)”行之后):
Small correction to the code above.
It does not close input stream, while Apache lib assumes that stream is managed by calling client.
See the fix below (put this code after the line 'aos.putArchiveEntry(entry)') :
这里的例子-> http://commons.apache.org/compress/examples.html 使用该方法putNextEntry(entry) 你似乎省略了。
the example here -> http://commons.apache.org/compress/examples.html uses the method putNextEntry(entry) which you seem to omit.
另请参阅我的答案此处
See also my answer here