Red*_*ite 6 java compression file-io tar apache-commons
我正在使用Apache Commons 1.4.1库来压缩和解压缩".tar.gz"
文件.
我在最后一点遇到麻烦 - 将a转换TarArchiveInputStream
成a FileOutputStream
.
奇怪的是,它在这条线上打破了:
FileOutputStream fout = new FileOutputStream(destPath);
Run Code Online (Sandbox Code Playgroud)
destPath
是一个具有Canonical路径的文件:C:\ Documents and Settings\Administrator\My Documents\JavaWorkspace\BackupUtility\untarred\Test\subdir\testinsub.txt
出错:
Exception in thread "main" java.io.IOException: The system cannot find the path specified
Run Code Online (Sandbox Code Playgroud)
知道它可能是什么?为什么它无法找到路径?
我正在附上下面的整个方法(其中大部分是从这里解除的).
private void untar(File dest) throws IOException {
dest.mkdir();
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
// tarIn is a TarArchiveInputStream
while (tarEntry != null) {// create a file with the same name as the tarEntry
File destPath = new File(dest.toString() + System.getProperty("file.separator") + tarEntry.getName());
System.out.println("working: " + destPath.getCanonicalPath());
if (tarEntry.isDirectory()) {
destPath.mkdirs();
} else {
destPath.createNewFile();
FileOutputStream fout = new FileOutputStream(destPath);
tarIn.read(new byte[(int) tarEntry.getSize()]);
fout.close();
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}
Run Code Online (Sandbox Code Playgroud)
tom*_*bee 16
您的程序有Java堆空间错误.所以我认为需要做一点改变.这是代码......
public static void uncompressTarGZ(File tarFile, File dest) throws IOException {
dest.mkdir();
TarArchiveInputStream tarIn = null;
tarIn = new TarArchiveInputStream(
new GzipCompressorInputStream(
new BufferedInputStream(
new FileInputStream(
tarFile
)
)
)
);
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
// tarIn is a TarArchiveInputStream
while (tarEntry != null) {// create a file with the same name as the tarEntry
File destPath = new File(dest, tarEntry.getName());
System.out.println("working: " + destPath.getCanonicalPath());
if (tarEntry.isDirectory()) {
destPath.mkdirs();
} else {
destPath.createNewFile();
//byte [] btoRead = new byte[(int)tarEntry.getSize()];
byte [] btoRead = new byte[1024];
//FileInputStream fin
// = new FileInputStream(destPath.getCanonicalPath());
BufferedOutputStream bout =
new BufferedOutputStream(new FileOutputStream(destPath));
int len = 0;
while((len = tarIn.read(btoRead)) != -1)
{
bout.write(btoRead,0,len);
}
bout.close();
btoRead = null;
}
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}
Run Code Online (Sandbox Code Playgroud)
祝好运