I received an zip file from customer. I have tried to extract with WinZip, but unable to extract zip file because that zip file was corrupt.
So I have tried to extract with Java Zip API and got the success. The code is listed below:
I am sure that the zip file having 5% to 10% part corrupt will be unzip with this code, but if it is having more corrupt then the code will not work.
import java.io.*;
import java.util.zip.*;
class ExtractCorruptZip {
public void extractZipFiles(String fileName) {
try {
String zipDir = "c:\\zip\\";
//String zipDir = "/home/usr/amit/zip/";
byte[] by = new byte[2048];
ZipInputStream ziStream = null;
ZipEntry zipEntry;
ziStream = new ZipInputStream(new FileInputStream(fileName));
zipEntry = ziStream.getNextEntry();
int count=1;
while (zipEntry != null) {
String entry = zipDir + zipEntry.getName();
entry = entry.replace('/', File.separatorChar).replace('\\', File.separatorChar);
int n;
FileOutputStream fOTStream;
File newFile = new File(entry);
if (zipEntry.isDirectory()) {
if (!newFile.mkdirs()) {
break;
}
zipEntry = ziStream.getNextEntry();
continue;
}
fOTStream = new FileOutputStream(entry);
while ((n = ziStream.read(by, 0, 2048)) > -1) {
fOTStream.write(by, 0, n);
}
System.out.println("Working....."+(++count));
fOTStream.close();
ziStream.closeEntry();
zipEntry = ziStream.getNextEntry();
}
ziStream.close();
System.out.println("DONE!!!!!");
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static void main(String[] ss) {
try {
String fileName = "w1.zip";
ExtractCorruptZip extractCorruptZip = new ExtractCorruptZip();
extractCorruptZip.extractZipFiles(fileName);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Find interesting stuff in Java, PHP and Dot Net.
Stop writing good code; Start writing Innovative code.
This site is from FUTURE. Science is powerful, but it cannot explain everything.
This site is for smart Primates & ROBOTS only (oh and A L I E N S).
Pages
- Home
- Send problem from my FaceBook Page
- Your FB bio/friends
- Auto Resume Broken File Uploader
- Screen Recorder
- Facebook App
- Screencast with Audio Capture
- Web Page Screen Capture As Image
- Add Watermark In Video
- Unique Code
- PDF Merger
- Unique Software
- Latest Posts
- Arbitrary Text in PDF
- MY ANDROID APP
- Torch in Android
Search This Blog
Top Ads
Friday, May 21, 2010
Subscribe to:
Post Comments (Atom)
Several days ago I downloaded some zip files with movie inside. But I couldn't open its. Reason of to my mind all of them was corrupted. Luckily my friend advised me - zip file corrupt. It solved my issues for seconds and without payment as far as I remembered.
ReplyDelete