Here you can find the source of addFileToZipOutputStream(File file, ZipOutputStream zipOs)
Parameter | Description |
---|---|
file | a parameter |
zipOs | a parameter |
public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException
//package com.java2s; /*/* ww w . j a va 2s . c o m*/ * The contents of this file are subject to the license and copyright * detailed in the LICENSE and NOTICE files at the root of the source * tree and available online at * * http://duracloud.org/license/ */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class Main { /** * Adds the specified file to the zip output stream. * * @param file * @param zipOs */ public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException { String fileName = file.getName(); try (FileInputStream fos = new FileInputStream(file)) { ZipEntry zipEntry = new ZipEntry(fileName); zipEntry.setSize(file.length()); zipEntry.setTime(System.currentTimeMillis()); zipOs.putNextEntry(zipEntry); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = fos.read(buf)) > 0) { zipOs.write(buf, 0, bytesRead); } zipOs.closeEntry(); fos.close(); } } }