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 :)
Thanks to all respondents that have been helping me out.
So the case is this gzip file (named as .zip extension) have a empty string at the end of file. So it threwing error like i mention in previous post. With below code that i get from my colleague, the problem is solved. I post it so maybe other user can use it in the future.
public void gunzipIt(){
byte[] buffer = new byte[1024];
boolean isValid = true;
try{
GZIPInputStream gzis =
new GZIPInputStream(new FileInputStream(INPUT_GZIP_FILE));
FileOutputStream out =
new FileOutputStream(OUTPUT_FILE);
while (isValid) {
int len;
try{
len = gzis.read(buffer);
}catch(Exception ex){
len = 0;
isValid = false;
}
if (len > 0) {
out.write(buffer, 0, len);
}else{
isValid = false;
}
}
gzis.close();
out.close();
System.out.println("Done");
}catch(IOException ex){
ex.printStackTrace();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.