import java.io.*;
import java.util.zip.*;
public class UnzipTest {
public static void unzipFile(File f) throws ZipException, IOException {
ZipInputStream zin = new ZipInputStream(new FileInputStream(f));
System.out.println(f.getAbsoluteFile());
String workingDir = f.getPath() + File.separator + "unziped";
byte buffer[] = new byte[4096];
int bytesRead;
ZipEntry entry = null;
while ((entry = zin.getNextEntry()) != null) {
String dirName = workingDir;
int endIndex = entry.getName().lastIndexOf(File.separatorChar);
if (endIndex != -1) {
dirName += entry.getName().substring(0, endIndex);
}
File newDir = new File(dirName);
// If the directory that this entry should be inflated under does
// not exist, create it.
if (!newDir.exists() && !newDir.mkdir()) { throw new ZipException("Could Not Create Directory " + dirName + "."); }
FileOutputStream fos = new FileOutputStream(workingDir + entry.getName());
// Copy Data From the Zip Entry to a File
while ((bytesRead = zin.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
}
zin.close();
}
public static void main(String[] args) {
try {
unzipFile(new File("test.zip"));
System.out.println("Success");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error - " + e.getMessage());
}
}
}
Pages
▼
Java- extract / unzip a zip file - working code example
How to Extract / unzip a zip file in Java - complete source code example .
No comments:
Post a Comment
Your Comment and Question will help to make this blog better...