i have receive a compressed file in zip extension. i cannot open it directly using windows explorer. i can extract it using 7Zip, it throws some error but the file still decompressed as expected. i can extract it using winrar, no error, file decompressed as expected.
then i tried to decompressed using java.util.unzip / zip4j.
java.util.zip code :
public static 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();
}
/**
* Extracts a zip entry (file entry)
* @param zipIn
* @param filePath
* @throws IOException
*/
private static void extractFile(ZipInputStream zipIn,
String filePath) throws IOException {
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[BUFFER_SIZE];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
with above code, no error happen, but i get a empty folder.
zip4j code :
String zipFilePath = "C:\\Juw\\JR\\file\\output\\020030214112016.zip";
//String zipFilePath = "C:\\Juw\\JR\\file\\output\\myFile.zip";
String destDirectory = "C:\\Juw\\JR\\file\\output\\targetUnzip";
try {
ZipFile zipFile = new ZipFile(zipFilePath);
zipFile.extractAll(destDirectory);
} catch (Exception ex) {
ex.printStackTrace();
}
and there's exception : net.lingala.zip4j.exception.ZipException: zip headers not found. probably not a zip file
if i tried to decompressed the file using winrar, and then i compressed it using windows built in zip feature. i can decompressed it succesfully using my code. and the size is different between mine and the client gave me. mine is 508kb, and the other one is 649kb.
question is : - are there any java library that utilize / as powerful winrar, that can extract the compressed file without error ? - what is the best practices to resolve this case ?
Many thanks in advance :)