Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created July 12, 2014 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomoTaka01/cf7bef93f748546e9351 to your computer and use it in GitHub Desktop.
Save tomoTaka01/cf7bef93f748546e9351 to your computer and use it in GitHub Desktop.
creating zip file without input files.
private void zipFromString(){
// zipファイルに含めるファイル(ディレクトリも指定)
List<String> files = Arrays.asList(
"dir1/test1.txt"
, "dir1/dir2/test2.txt");
ZipOutputStream out = null;
try {
// zipファイル指定
out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream("/Users/tomo/ziptest/myzip.zip")));
for (String file : files) {
ZipEntry entry = new ZipEntry(file); // zipのエントリ作成
out.putNextEntry(entry);
String data = String.format("file name:%s", file);
out.write(data.getBytes()); // zipエントリにデータを書く
}
} catch (Exception ex) {
Logger.getLogger(ZipSample.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (out != null){
try {
out.close();
} catch (IOException ignore) {}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment