import java.io.*;
import java.util.zip.*;
public class Unzip {
public static void unzipTo(String zipName, String dirName) throws Exception {
final int BUFFER = 2048;
File dir = new File(dirName);
if (!dir.exists())
dir.mkdir();
if (!dirName.endsWith("\\") && !dirName.endsWith("/")) {
dirName += "/";
}
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(zipName);
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
checksum));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " + entry + " to " + dirName
+ entry.getName());
int count;
byte data[] = new byte[BUFFER];
// write the files to the disk
FileOutputStream fos = new FileOutputStream(dirName
+ entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
zis.close();
System.out.println("Checksum: " + checksum.getChecksum().getValue());
}
public static final void main(String[] args) {
try {
String targetDir = "D:\\APP\\workspace\\chunkcode\\tmp";
String zipFile = "D:/APP/workspace/chunkcode/test.zip";
unzipTo(zipFile, targetDir);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The code is based from this article.
No comments:
Post a Comment