【发布时间】:2014-09-11 22:16:15
【问题描述】:
我正在使用来自网络的以下代码
File dir = new File(dest.getAbsolutePath(), archiveName);
// create output directory if it doesn't exist
if (!dir.exists()) {
dir.mkdirs();
}
System.err.println(pArchivePath);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(pArchivePath);
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = zipEntry.getName();
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();
System.out.printf("name: %-20s | size: %6d | compressed size: %6d\n",
name, size, compressedSize);
File file = new File(name);
if (name.endsWith("/")) {
System.err.println("make dir " + name);
file.mkdirs();
continue;
}
File parent = file.getParentFile();
if (parent != null) {
parent.mkdirs();
}
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
}
is.close();
fos.close();
}
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (zipFile != null) {
try {
zipFile.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解压缩档案。它包含 7zip 或 Winrar 作为 .zip 存档,但重命名为 .qtz(但我不认为这会导致问题..) 因此,如果我运行代码来解压缩我的存档,一切正常:我在 sysout/err 上获得列出所有文件的输出,也没有发生异常,但是如果我查看目标目录......它是空的 - 只有根文件夹存在.
我也用过
Runtime.getRuntime().exec(String.format("unzip %s -d %s", pArchivePath, dest.getPath()));
但我不能再使用它了,因为新进程已启动,并且在 java 代码中的解压缩过程之后,我将继续处理存档。
那么问题是.. 为什么这种和平的代码不起作用?有很多类似的例子,但没有一个对我有用。
br,菲利普
编辑:以下解决了我的问题
File file = new File(dir.getParent(), name);
所以我没有为这个文件设置正确的父路径。
【问题讨论】: