Java生成Zipファイル
1431 ワード
commons-io
commons-io
2.5
import org.apache.commons.io.IOUtils;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.*;
private void reZipCsvFiles(String targetZipRealPath, List targetFilePathList) throws Exception {
File targetZipFile = new File(targetZipRealPath);
InputStream in = null;
FileOutputStream fos = null;
ZipOutputStream zipOutputStream = null;
try {
fos = new FileOutputStream(targetZipFile);
zipOutputStream = new ZipOutputStream(fos);
for (String csvFilePath: targetFilePathList) {
in = new FileInputStream(csvFilePath);
String csvFileName = csvFilePath.substring(csvFilePath.lastIndexOf(File.separator) + 1);
zipOutputStream.putNextEntry(new ZipEntry(csvFileName));
IOUtils.copy(in, zipOutputStream);
zipOutputStream.closeEntry();
in.close();
}
} finally {
if (zipOutputStream != null) {
zipOutputStream.close();
}
if (fos != null) {
fos.close();
}
if (in != null) {
in.close();
}
}
}