Webシステム開発の雑多なアウトプット

AWS、プログラム、OSS等を中心に良かった本も。

ZipDecomp

    /** ########################################################################
    # クラス名 : ZipDecomp
    # 概要     : zipファイルを解凍する
    # 引数     : String filename  (ファイルフルパス)
    # 戻り値   : String zipFilePath (zipファイルフルパス)
    ############################################################################# **/
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class ZipDecomp {
    
       private static final int TOOBIG = 0x6400000; // 100MB
       private static final int BUFFER = 512;
    
       public static String Decomp(String filename) {
    
          BufferedOutputStream dest = null;
          FileInputStream fis = null;
          ZipInputStream zis = null;
          FileOutputStream fos = null;
          ZipEntry entry = null;
          String zipFilePath = null;
    
          try {
             fis = new FileInputStream(filename);
             zis = new ZipInputStream(new BufferedInputStream(fis));
    
             // 解凍先(ファイルパス)取得
             File file = new File(filename);
             String dirPath = file.getParent();
    
             while ((entry = zis.getNextEntry()) != null) {
    
                int count;
                byte data[] = new byte[BUFFER];
    
                // ファイルサイズが大きすぎなければ、ディスクに書き出す
                if (entry.getSize() > TOOBIG) {
                  throw new IllegalStateException("File to be unzipped is huge.");
                }
                if (entry.getSize() == -1) {
                  throw new IllegalStateException("File to be unzipped might be huge.");
                }
                zipFilePath = dirPath + "\\" + entry.getName();
                fos = new FileOutputStream(zipFilePath);
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                   dest.write(data, 0, count);
                }
                System.out.println("Extracting : " + entry);
                dest.flush();
             }
          } catch(Exception ex) {
             ex.printStackTrace();
          }finally {
             try { dest.close(); } catch (Throwable th){  }
             try { fos.close(); } catch (Throwable th){  }
             try { zis.close(); } catch (Throwable th){  }
             try { fis.close(); } catch (Throwable th){  }
             return zipFilePath;
          }
       }
    }