问题
I would like just to extract a normal zip file, but it keeps failing. This is my code I'm using now:
private File downloadPath = new File(Environment.getExternalStorageDirectory() + "/Test/file.zip");
private File unzipLoc = new File(Environment.getExternalStorageDirectory() + "/Test/");
FileInputStream fin = new FileInputStream(downloadPath);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null)
{
FileOutputStream fout = new FileOutputStream(unzipLoc + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read())
{
fout.write(c);
}
zin.closeEntry();
fout.close();
}
zin.close();
It fails on the 'zin.getNextEntry()' part. Error: java.util.zip.ZipException: Cannot read version Any ideas? Thx!
回答1:
Looks like your zip file is newer than your 'unzipping library'.
If you read the source:
ZipInputStream (search for new ZipException("Cannot read version"))
It shows you it checks the zip files version. Then looking at Wikipedia it shows this is the minimum version needed to extract the zip.
Check your zip file and re-save it with a lower version of your zip software / zip it again with no compression to test
Alternately update your Zip library (which you can't do as your using the internal android zip library).
回答2:
Just to add to the above answers for the solution to the problem and why was it created in my case. Yes Special thanks to Mr Bungle and Mr Blundell in helping to understand and solve the problem quickly.
I encountered this exception stating "java.util.ZipException Cannot read local header version 45" while I was unzipping files on an SDCard. The zip file was downloaded from the DOT NET Server, and the server was using SharpZipLib library to unzip the files. From the exception it clearly means that the zipping and unzipping libraries have a different version.
Solution:
Basically the library SharpZipLib uses Zip64 (extended format of zip file format to zip the files) Which is incompatible with java.util.zip package available in android and on Java releases prior to 7.
So if you switch of the Zip64 on the server which is zipping the files then it will become compatible with the android java.util.zip package and will easily be unzipped. If you add the following line on the server it will solve your problem:
ZipOutputStream.UseZip64 = UseZip64.Off
来源:https://stackoverflow.com/questions/7148864/unzipping-zip-file-gives-java-util-zip-zipexception-cannot-read-version-or-j