2007年9月8日土曜日

JavaでZip圧縮する方法

<サンプル>

ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
new FileOutputStream("sample.zip")));


//圧縮するファイルを書き込む。(A)
out.putNextEntry(new ZipEntry("sample.txt"));
BufferedInputStream in = new BufferedInputStream(new FileInputStream("sample.txt"));

while(true) {
int b = in.read();
if (b < 0) {
break;
}
out.write(b);
}

in.close();

out.closeEntry();

//他のファイルを圧縮する場合はから繰り返す。(A)

out.close();

0 件のコメント: