Created
July 12, 2014 02:30
-
-
Save tomoTaka01/cf7bef93f748546e9351 to your computer and use it in GitHub Desktop.
creating zip file without input files.
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
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