Created
December 2, 2012 01:19
-
-
Save codelance/4186364 to your computer and use it in GitHub Desktop.
Unzip
This file contains hidden or 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.BufferedInputStream; | |
| import java.io.BufferedOutputStream; | |
| import java.io.File; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.util.Enumeration; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipFile; | |
| import javax.swing.JFileChooser; | |
| import javax.swing.filechooser.FileNameExtensionFilter; | |
| public class Unzip { | |
| /** | |
| * @param args | |
| * @throws FileNotFoundException | |
| */ | |
| public static void main(String[] args) { | |
| JFileChooser chooser = new JFileChooser(); | |
| chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | |
| int returnVal = chooser.showOpenDialog(null); | |
| if (returnVal != JFileChooser.APPROVE_OPTION) { | |
| return; | |
| } | |
| String path = chooser.getSelectedFile().getAbsolutePath(); | |
| try { | |
| File f = new File(path); | |
| File[] contents = f.listFiles(); | |
| for (int i = 0; i < contents.length; i++) { | |
| if (contents[i].getName().contains(".zip")) { | |
| ZipFile zipFile = new ZipFile(contents[i]); | |
| Enumeration<? extends ZipEntry> enumeration = zipFile.entries(); | |
| String fileName = contents[i].getName(); | |
| int period = fileName.indexOf('.'); | |
| fileName = fileName.substring(0, period); | |
| File newfolder = new File(path + "\\" +fileName); | |
| Boolean result = newfolder.mkdir(); | |
| while (enumeration.hasMoreElements()) | |
| { | |
| ZipEntry zipEntry = (ZipEntry) enumeration.nextElement(); | |
| //System.out.println("Unzipping: " + zipEntry.getName()); | |
| BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry)); | |
| int size; | |
| byte[] buffer = new byte[2048]; | |
| BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(path + "\\" + newfolder.getName() + "\\" + zipEntry.getName()), buffer.length); | |
| while ((size = bis.read(buffer, 0, buffer.length)) != -1) { | |
| bos.write(buffer, 0, size); | |
| } | |
| bos.flush(); | |
| bos.close(); | |
| bis.close(); | |
| } | |
| contents[i].delete(); | |
| } | |
| } | |
| } catch (FileNotFoundException ex) { | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment