最近、ふとん圧縮袋の通販見ないねぇw
ZipInputStreamを使って、ZIPファイルを丸ごと解凍するサンプル
ZipInputStreamを使って、ZIPファイルを丸ごと解凍します。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class ZipDecompressTest {     private static final int BUFFER_SIZE = 4096;     public static void main(String[] args) {         String srcPath = "C:\\work\\in\\in.zip"; // 解凍するZIPファイルのパス         String destPath = "C:\\work\\out"; // 解凍先のフォルダのパス         try {             decompressZip(srcPath, destPath);         } catch (IOException e) {             e.printStackTrace();         }     }     public static void decompressZip(String srcPath, String destPath) throws IOException {         System.out.println("解凍対象フォルダ:"+srcPath);         System.out.println("解凍先ファイル:"+destPath);         //フォルダ内のファイルとサブフォルダを再帰的にZIPに追加         try(FileInputStream fis = new FileInputStream(srcPath);             ZipInputStream zis = new ZipInputStream(fis);) {             decompressEntry(destPath, zis);         }         System.out.println("処理終了..");     }     private static void decompressEntry(String destPath, ZipInputStream zis) throws IOException {         Path checkPath = Paths.get(destPath);         if(Files.exists(checkPath) == false) {             Files.createDirectories(checkPath);         }         ZipEntry entry = null;         while ((entry = zis.getNextEntry()) != null) {             String entryName = entry.getName();             System.out.println(destPath + File.separator + entryName);             Path filePath = Paths.get(destPath + File.separator + entryName);             if (!entry.isDirectory()) {                 Files.createDirectories(filePath.getParent());                 try (FileOutputStream fos = new FileOutputStream(filePath.toString())) {                     byte[] buffer = new byte[BUFFER_SIZE];                     int bytes = -1;                     while ((bytes = zis.read(buffer)) != -1) {                         fos.write(buffer, 0, bytes);                     }                 }             }             zis.closeEntry();         }     } } | 
実行結果
ZIPファイルが解凍され、解凍先フォルダにファイルを展開します。
| 1 2 3 4 5 6 7 | 解凍対象フォルダ:C:\work\in\in.zip 解凍先ファイル:C:\work\out C:\work\out\in/a.txt C:\work\out\in/hoge/b.txt C:\work\out\in/hoge/かきくけこ.txt C:\work\out\in/あいうえお.txt 処理終了.. | 
サンプルの解説
このサンプルでは、こちらで圧縮したものの逆で、解凍します。
ZIPファイルを解凍するには、ZipInputStreamを使います。
 ZipEntryが内部に圧縮されるているもの。
 このZipEntry単位で、ZipInputStreamのgetNextEntry、read、closeEntryを繰り返します。













