使用apache commons压缩的tar问题 [英] Tar problem with apache commons compress
问题描述
我很难用tar压缩库来压缩一些文件。
I'm having a hard time trying to tar some files using the compress library.
我的代码如下,来自commons.compress wiki exemples:
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
b $ b
此外,归档文件的大小比原始文件稍小,这是不正常的tar,所以有一个问题...
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
推荐答案
您未关闭 TarArchiveOutputStream
。在之后添加
aos.close()
aos.finish()
You're not closing the TarArchiveOutputStream
. Add aos.close()
after aos.finish()
这篇关于使用apache commons压缩的tar问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!