我有一个压缩文件 (从ownCloud实例下载),不能使用java.util.zip的Java库(java版本"1.8.0_66")解压缩。如果我尝试这样做,将引发以下异常:
java.util.zip.ZipException: invalid LOC header (bad signature)
当发出Linux 'file‘命令时,它输出以下内容:
so-example.zip: Zip archive data, at least v3.0 to extract
经过一些尝试和错误之后,我发现"v2.0“应该是不可压缩的文件可以由Java处理,没有任何问题。
用于解压缩的代码的一个最小示例如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class PlaygroundZip{
public static void main(String[] args) {
String filename = "/tmp/so-example.zip";
byte[] buffer = new byte[1024];
try {
ZipFile zipFile = new ZipFile(filename);
Enumeration entries = zipFile.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
String currName = entry.getName();
System.out.println("File: " + currName);
if(entry.isDirectory()) {
new File(currName).mkdirs();
} else{
InputStream zis = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(currName);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
}
}
}
如何使用标准Java库或任何其他方法处理这些文件?
发布于 2015-11-02 14:37:20
到目前为止,我发现解压缩这些文件的唯一方法是使用一个系统命令并使用" UnZip“命令行工具(Ubuntu12.04LTS,UnZip 6.00)。因为这是在服务器应用程序中使用的,所以这将是一个可以接受的黑客攻击,尽管离优雅还有很远的距离。
Process p = Runtime.getRuntime().exec("unzip " + filename + " -d /tmp/mydir");
https://stackoverflow.com/questions/33480085
复制相似问题