java.util.zip.ZipException:无效的压缩方法
我在处理Mac OS X 10.7.3上的zip文件时遇到了一些问题。java.util.zip.ZipException:无效的压缩方法
我从第三方收到一个zip文件,我必须处理该文件。我的代码使用ZipInputStream来做到这一点。此代码之前已经使用过几次,没有任何问题,但是对于这个特定的zip文件却失败了。这是我得到的错误是如下:
java.util.zip.ZipException: invalid compression method
at java.util.zip.ZipInputStream.read(ZipInputStream.java:185)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)
at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
我GOOGLE了一下,我可以看到,有一些问题与ZipInputStream,例如this one。
我在*上也发现了一些相关的问题,例如: this one。但没有适当的,可接受的/可接受的答案。
我有几个问题:
- 有没有人发现这方面的任何具体的解决方案?如同任何酸酯 更新或具有相同的功能 全在一起的不同JAR但没有问题
- 在此link,用户phobuz1提到“如果 非标准压缩方法(方法6)”被使用,那么这个 问题就会发生。有没有办法找出使用哪种压缩方法 ?所以我可以肯定失败的原因?
请注意,与某些用户一样,如果我在本地计算机上解压缩文件并重新压缩文件,它的工作原理完全正常。
编辑1:
的文件,我得到的是在.zip
格式,我不知道哪个OS /实用程序他们正在使用压缩它。在我的本地机器我使用附带的Mac OS X的内置压缩工具
API JAVADOC:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/package-summary.html
这是真正压缩文件:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/zip/ZipEntry.html
每一个接口上,你有一个选项来获取和设置压缩方法。
祝你好运!
谢谢布兰德,我会试试看。 – Bhushan 2012-03-12 23:50:37
我在我的Windows XP操作系统上使用以下Java代码压缩文件夹。作为一个附注,它可能至少对你有用。
//add folder to the zip file
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception
{
File folder = new File(srcFolder);
//check the empty folder
if (folder.list().length == 0)
{
System.out.println(folder.getName());
addFileToZip(path , srcFolder, zip,true);
}
else
{
//list the files in the folder
for (String fileName : folder.list())
{
if (path.equals(""))
{
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip,false);
}
else
{
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip,false);
}
}
}
}
//recursively add files to the zip files
private void addFileToZip(String path, String srcFile, ZipOutputStream zip,boolean flag)throws Exception
{
//create the file object for inputs
File folder = new File(srcFile);
//if the folder is empty add empty folder to the Zip file
if (flag==true)
{
zip.putNextEntry(new ZipEntry(path + "/" +folder.getName() + "/"));
}
else
{
//if the current name is directory, recursively traverse it to get the files
if (folder.isDirectory())
{
addFolderToZip(path, srcFile, zip); //if folder is not empty
}
else
{
//write the file to the output
byte[] buf = new byte[1024];
int len;
FileInputStream in = new FileInputStream(srcFile);
zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
while ((len = in.read(buf)) > 0)
{
zip.write(buf, 0, len); //Write the Result
}
}
}
}
//zip the folders
private void zipFolder(String srcFolder, String destZipFile) throws Exception
{
//create the output stream to zip file result
FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter);
//add the folder to the zip
addFolderToZip("", srcFolder, zip);
//close the zip objects
zip.flush();
zip.close();
}
private boolean zipFiles(String srcFolder, String destZipFile) throws Exception
{
boolean result=false;
System.out.println("Program Start zipping the given files");
//send to the zip procedure
zipFolder(srcFolder,destZipFile);
result=true;
System.out.println("Given files are successfully zipped");
return result;
}
在此代码,就需要通过传递两个参数来调用前述方法zipFiles(String srcFolder, String destZipFile)
。第一个参数表示要压缩的文件夹,第二个参数destZipFile
表示您的目标zip文件夹。
以下代码是解压缩压缩文件夹。
private void unzipFolder(String file) throws FileNotFoundException, IOException
{
File zipFile=new File("YourZipFolder.zip");
File extractDir=new File("YourDestinationFolder");
extractDir.mkdirs();
ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile));
try
{
ZipEntry entry;
while ((entry = inputStream.getNextEntry()) != null)
{
StringBuilder sb = new StringBuilder();
sb.append("Extracting ");
sb.append(entry.isDirectory() ? "directory " : "file ");
sb.append(entry.getName());
sb.append(" ...");
System.out.println(sb.toString());
File unzippedFile = new File(extractDir, entry.getName());
if (!entry.isDirectory())
{
if (unzippedFile.getParentFile() != null)
{
unzippedFile.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(unzippedFile);
try
{
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
}
}
finally
{
if (outputStream != null)
{
outputStream.close();
}
}
}
else
{
unzippedFile.mkdirs();
}
}
}
finally
{
if (inputStream != null)
{
inputStream.close();
}
}
}
您使用哪种zip压缩方法?我可以知道吗? – Lion 2012-03-12 23:36:31
@Lion:我得到的文件是'.zip'格式,我不知道他们使用哪个操作系统/实用程序来压缩它。在我的本地机器上,我使用了Mac OS X附带的内置zip实用程序。感谢您提出一个很好的解释问题,更新问题。 – Bhushan 2012-03-12 23:38:58