by

How to generate a zip file from a List of File

public static void addToZipFile(File file, ZipOutputStream zos) throws FileNotFoundException, IOException {
  FileInputStream fis = new FileInputStream(file);
  ZipEntry zipEntry = new ZipEntry(file.getName());
  zos.putNextEntry(zipEntry);

  byte[] bytes = new byte[1024];
  int length;
  while ((length = fis.read(bytes)) >= 0) {
    zos.write(bytes, 0, length);
  }

  zos.closeEntry();
  fis.close();
}
FileOutputStream fos = new FileOutputStream(C:\myzipfile.zip);
ZipOutputStream zos = new ZipOutputStream(fos);

for(File f : filesToAdd){
  Util.addToZipFile(f, zos);
}

zos.close();
fos.close();

Escreva um comentário

Comment