Reputation: 2386
I have downloaded the zip file and when I am unzipping the files I am getting the above exception. Below is my structure after I have unzipped my zip file.
zip structure(After unzip): folder1 subfolder1 sub1 sub2 subfolder2 subf1 subf2
String inputPath = Environment.getExternalStorageDirectory().getPath()+"/"+arrayListQuestions.get(0).getQuestion_asset_name();
Log.e("inputPath",inputPath);
String outputPath = Environment.getExternalStorageDirectory().getPath()+"/unzip/";
Log.e("outPath",outputPath);
ZipManager zipManager = new ZipManager();
try {
zipManager.unzip(inputPath, outputPath);
} catch (IOException e) {
e.printStackTrace();
Log.e("error_unzip",e.getLocalizedMessage());
}
UnzipFunction:
public void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
Upvotes: 0
Views: 511
Reputation: 840
The solution is to use ZipFile instead of ZipInputStream
. ZipFile
appears to handle name-less local file headers gracefully.
Note that the disadvantage of ZipFile is that it cannot process arbitrary InputStream
s and only takes files.
Also note that the problem appears to be specific to Android. I don't get this exception with Oracle's JRE.
Upvotes: 0