Java ZipEntry Add addZipEntry(ZipOutputStream zipOut, File file, String prefix)

Here you can find the source of addZipEntry(ZipOutputStream zipOut, File file, String prefix)

Description

add Zip Entry

License

Open Source License

Declaration

private static void addZipEntry(ZipOutputStream zipOut, File file, String prefix)
        throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  ww. j a  v  a 2  s .c  o m*/
 *     The Chisel Group, University of Victoria
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * @author Shawn Minto
     */
    private static void addZipEntry(ZipOutputStream zipOut, File file, String prefix)
            throws FileNotFoundException, IOException {

        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        // Compress the files
        FileInputStream in = new FileInputStream(file);

        // Add ZIP entry to output stream.
        addZipEntryRecursive(zipOut, file, prefix);

        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            zipOut.write(buf, 0, len);
        }

        // Complete the entry
        zipOut.closeEntry();
        in.close();
    }

    /**
     * If the file is a file, it is added to the output stream with the given prefix.
     * If the file is a directory, it calls this method.
     * @param file    the file or directory to add to the output stream
     * @param prefix the prefix - is either blank or ends with a forwardslash e.g. "dir/"
     * @throws IOException
     */
    private static void addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix) throws IOException {
        if (file.isDirectory()) {
            String[] files = file.list();
            for (String filename : files) {
                addZipEntryRecursive(zipOut, new File(file, filename), prefix + file.getName() + "/");
            }
        } else {
            zipOut.putNextEntry(new ZipEntry(prefix + file.getName()));
        }
    }
}

Related

  1. addZipEntry(String pathName, byte[] contents, ZipOutputStream zos)
  2. addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)
  3. addZipEntry(ZipOutputStream zos, File fileToZip, File baseFolder)
  4. addZipEntryFromStream(InputStream is, ZipOutputStream os, String filename)
  5. addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix)