「Java入門」ZipOutputStreamでファイルをzipに圧縮するコード
javaコード
private void FiletoZipFunc(List<File> fileList) throws IOException{
if(null != fileList && !fileList.isEmpty()){
byte[] buf = new byte[1024];
String url = REAL_PATH + “/" + EXPORT_URL + “/" + TABLE_NAME;
String fi = url + “/" + TABLE_NAME + “.zip";
FileOutputStream os = new FileOutputStream(fi);
ZipOutputStream zipOut = new ZipOutputStream(os);
for(File file : fileList){
FileInputStream in = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(file.getName()));
int len;
while((len = in.read(buf)) > 0){
zipOut.write(buf, 0, len);
}
zipOut.closeEntry();
in.close();
file.delete();//ファイル削除
}
zipOut.close();
}
}