Java Zip File zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)

Here you can find the source of zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)

Description

Add a file into a zip.

License

Open Source License

Parameter

Parameter Description
filePath the file to be added in the zip.
iBaseFolderLength the index in the directoryName from which starts the actual zip entry name.
jos the stream to write into.
crc the CRC32 of all zipentries. Can be null if no crc is needed.

Exception

Parameter Description
IOException if the zip file cannot be written.

Declaration

protected static void zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)
        throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w  w . ja v  a2  s. c om*/
 * ProActive Parallel Suite(TM):
 * The Open Source library for parallel and distributed
 * Workflows & Scheduling, Orchestration, Cloud Automation
 * and Big Data Analysis on Enterprise Grids & Clouds.
 *
 * Copyright (c) 2007 - 2017 ActiveEon
 * Contact: contact@activeeon.com
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation: version 3 of
 * the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 */

import java.io.BufferedInputStream;

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

import java.io.IOException;

import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Add a file into a zip.
     * @param filePath the file to be added in the zip.
     * @param iBaseFolderLength the index in the directoryName from which starts the actual zip entry name.
     * @param jos the stream to write into.
     * @param crc the CRC32 of all zipentries. Can be null if no crc is needed.
     * @throws IOException if the zip file cannot be written.
     */
    protected static void zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)
            throws IOException {
        try {
            FileInputStream fis = new FileInputStream(filePath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            String fileNameEntry = filePath.substring(iBaseFolderLength).replace(File.separatorChar, '/');
            ZipEntry fileEntry = new ZipEntry(fileNameEntry);
            jos.putNextEntry(fileEntry);
            byte[] data = new byte[1024];
            int byteCount;
            while ((byteCount = bis.read(data, 0, 1024)) > -1) {
                if (crc != null) {
                    crc.update(data);
                }
                jos.write(data, 0, byteCount);
            }
            jos.closeEntry();
            fis.close();
        } catch (ZipException e) {
            // TODO Other exceptions ?
            // Duplicate entry : ignore it.
        }
    }
}

Related

  1. zipFile(File sourceFile)
  2. zipFile(File zipfile, ZipOutputStream zos, String name)
  3. zipFile(final File source)
  4. zipFile(final File target, final ZipOutputStream zip, final File file, final String path)
  5. zipFile(String filedir, String zippath)
  6. zipFile(String filePath, String fileName)
  7. zipFile(String inPath, String outPath, String nameInsideZip)