Last active
August 29, 2015 14:02
-
-
Save daichan4649/e6269669631c0a13ef56 to your computer and use it in GitHub Desktop.
unzip (for Java)
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
public static void unzip(String zipFilePath, File dstDir) { | |
try { | |
final ZipFile zipFile = new ZipFile(zipFilePath); | |
Enumeration<? extends ZipEntry> enumZip = zipFile.entries(); | |
while (enumZip.hasMoreElements()) { | |
final ZipEntry zipEntry = enumZip.nextElement(); | |
// 出力先設定 | |
File dstFile = new File(dstDir, zipEntry.getName()); | |
if (dstFile.isDirectory()) { | |
continue; | |
} | |
File parentDir = dstFile.getParentFile(); | |
if (!parentDir.exists()) { | |
parentDir.mkdirs(); | |
} | |
System.out.println(dstFile.getPath()); | |
// 出力 | |
writeZipEntry2File(zipFile, zipEntry, dstFile); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void writeZipEntry2File(ZipFile zipFile, ZipEntry zipEntry, File dstFile) { | |
try { | |
writeStream2File(zipFile.getInputStream(zipEntry), dstFile); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void writeStream2File(InputStream srcStream, File dstFile) { | |
BufferedInputStream bis = null; | |
BufferedOutputStream bos = null; | |
try { | |
bis = new BufferedInputStream(srcStream); | |
bos = new BufferedOutputStream(new FileOutputStream(dstFile)); | |
byte[] buffer = new byte[1024]; | |
int readSize = 0; | |
while ((readSize = bis.read(buffer)) != -1) { | |
bos.write(buffer, 0, readSize); | |
} | |
bos.flush(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} finally { | |
if (bos != null) { | |
try { | |
bos.close(); | |
} catch (IOException e) { | |
} | |
} | |
if (bis != null) { | |
try { | |
bis.close(); | |
} catch (IOException e) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment