SHARE
TWEET

ZIPファイル作成サンプル

EnderAlice Mar 29th, 2015 256 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.FileOutputStream;
  3. import java.nio.charset.Charset;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.zip.CRC32;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8.  
  9. public final class Zip
  10. {
  11.         private static final byte[] DATA = "\uFEFFとんかつを作るにはな、こうやって油の中に指をアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアアーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥".getBytes(StandardCharsets.UTF_16LE);
  12.         private static final CRC32 CRC = new CRC32();
  13.  
  14.         public static void main(String argv[]) throws Exception
  15.         {
  16.                 ZipOutputStream oz = createZip("TEST.ZIP");
  17.                 addEntry(oz, "♥♥♥♥♥♥♥♥.TXT", DATA);
  18.                 oz.close();
  19.         }
  20.  
  21.         private static ZipOutputStream createZip(String file) throws Exception
  22.         {
  23.                 return createZip(file, null);
  24.         }
  25.  
  26.         private static ZipOutputStream createZip(String file, Charset filenameEncoding) throws Exception
  27.         {
  28.                 File f = new File(file);
  29.                 FileOutputStream of = new FileOutputStream(f);
  30.                 ZipOutputStream oz = (filenameEncoding != null) ? new ZipOutputStream(of, filenameEncoding) : new ZipOutputStream(of);
  31.                 oz.setMethod(ZipOutputStream.STORED);
  32.                 return oz;
  33.         }
  34.  
  35.         private static void addEntry(ZipOutputStream oz, String name, byte[] data) throws Exception
  36.         {
  37.                 ZipEntry e = new ZipEntry(name);
  38.                 e.setSize(data.length);
  39.                 CRC.reset();
  40.                 CRC.update(data);
  41.                 e.setCrc(CRC.getValue());
  42.                 oz.putNextEntry(e);
  43.                 oz.write(data);
  44.                 CRC.reset();
  45.         }
  46. }
RAW Paste Data
Top