Java Zip Files addFilesToZip(File zipFile, File[] files, String[] fileNames)

Here you can find the source of addFilesToZip(File zipFile, File[] files, String[] fileNames)

Description

add Files To Zip

License

Open Source License

Declaration

public static void addFilesToZip(File zipFile, File[] files,
            String[] fileNames) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w w  w.ja  v a2 s .  co  m*/
 *  odt2braille - Braille authoring in OpenOffice.org.
 *
 *  Copyright (c) 2010-2011 by DocArch <http://www.docarch.be>.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.io.FileOutputStream;
import java.io.FileInputStream;

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

public class Main {
    public static void addFilesToZip(File zipFile, File[] files,
            String[] fileNames) throws IOException {

        File tempFile = new File(zipFile.getAbsoluteFile() + ".temp");
        if (!zipFile.renameTo(tempFile)) {
            throw new RuntimeException("could not rename");
        }
        byte[] buf = new byte[1024];
        ZipInputStream zin = new ZipInputStream(new FileInputStream(
                tempFile));
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
                zipFile));
        ZipEntry entry = zin.getNextEntry();
        while (entry != null) {
            String name = entry.getName();
            boolean notInFiles = true;
            for (String f : fileNames) {
                if (f.equals(name)) {
                    notInFiles = false;
                    break;
                }
            }
            if (notInFiles) {
                out.putNextEntry(new ZipEntry(name));
                int len;
                while ((len = zin.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            }
            entry = zin.getNextEntry();
        }
        zin.close();
        for (int i = 0; i < files.length; i++) {
            InputStream in = new FileInputStream(files[i]);
            out.putNextEntry(new ZipEntry(fileNames[i]));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
        tempFile.delete();
    }
}

Related

  1. addFileToZip(File file, String entryName, ZipOutputStream zos)
  2. addFileToZip(File file, String entryName, ZipOutputStream zOut)
  3. addFileToZip(File file, String parentFolderName, ZipOutputStream zip)
  4. addFileToZip(File file, ZipOutputStream zos)