Created
November 2, 2011 10:16
-
-
Save tai2/1333328 to your computer and use it in GitHub Desktop.
Unzip an archive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.InputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipFile; | |
public class Unzip { | |
public static void unzip(String path) throws IOException { | |
File input_file = new File(path); | |
ZipFile archive = new ZipFile(input_file); | |
Enumeration<? extends ZipEntry> entries = archive.entries(); | |
byte[] buffer = new byte[8192]; | |
while(entries.hasMoreElements()) { | |
ZipEntry entry = (ZipEntry)entries.nextElement(); | |
File file = new File(entry.getName()); | |
if (entry.isDirectory()) { | |
file.mkdirs(); | |
} else { | |
File parent = file.getParentFile(); | |
if (parent != null) { | |
file.getParentFile().mkdirs(); | |
} | |
File output_file = input_file.getParentFile() != null ? | |
new File(input_file.getParentFile(), entry.getName()) : | |
new File(entry.getName()); | |
InputStream istream = archive.getInputStream(entry); | |
FileOutputStream ostream = new FileOutputStream(output_file); | |
int len; | |
while((len = istream.read(buffer)) > 0) { | |
ostream.write(buffer, 0, len); | |
} | |
istream.close(); | |
ostream.close(); | |
} | |
} | |
archive.close(); | |
} | |
public static void main(String[] args) { | |
if (args.length < 1) { | |
System.err.println("no file"); | |
} | |
try { | |
unzip(args[0]); | |
} catch (Exception e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment