Java Zip Files addFileToZipOutputStream(File file, ZipOutputStream zipOs)

Here you can find the source of addFileToZipOutputStream(File file, ZipOutputStream zipOs)

Description

Adds the specified file to the zip output stream.

License

Apache License

Parameter

Parameter Description
file a parameter
zipOs a parameter

Declaration

public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException 

Method Source Code


//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();
        }
    }
}

Related

  1. addFileToZip(String path, String srcFile, ZipOutputStream zip)
  2. addFileToZip(ZipOutputStream out, InputStream in, String entry)
  3. addFileToZip(ZipOutputStream zipOutputStream, File file, String basePath)
  4. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
  5. addFileToZip(ZipOutputStream zos, File file, File rootDir)
  6. addToZip(byte[] zip, String file, String fileName)
  7. addToZip(File directoryToZip, File file, ZipOutputStream zos)
  8. addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)
  9. addToZip(File file, ZipOutputStream out, String folder)