Java Zip File zip(String zipPath, Map input)

Here you can find the source of zip(String zipPath, Map input)

Description

Create a zip file and add contents.

License

Open Source License

Parameter

Parameter Description
zipPath the zip file
input a map of root directories and corresponding filters. Each root directory will be searched, and any files that pass the filter will be added to the zip file.

Exception

Parameter Description
IOException an exception

Declaration

public static void zip(String zipPath, Map<File, FileFilter> input) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2010 BEA Systems, Inc, IBM Corporation, and others
 *
 * 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  .  ja va2  s .c  o m*/
 *    mkaufman@bea.com - initial API and implementation
 *    
 *******************************************************************************/

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Map;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**
     * Create a zip file and add contents.
     * @param zipPath the zip file
     * @param input a map of root directories and corresponding filters.  Each
     * root directory will be searched, and any files that pass the filter will
     * be added to the zip file.
     * @throws IOException
     */
    public static void zip(String zipPath, Map<File, FileFilter> input) throws IOException {
        ZipOutputStream zip = null;
        try {
            zip = new ZipOutputStream(new FileOutputStream(zipPath));
            // +1 for last slash
            for (Map.Entry<File, FileFilter> e : input.entrySet()) {
                zip(zip, e.getKey(), e.getKey().getPath().length() + 1, e.getValue());
            }
        } finally {
            if (zip != null) {
                zip.close();
            }
        }
    }

    private static void zip(ZipOutputStream zip, File dir, int rootPathLength, FileFilter filter)
            throws IOException {
        String[] list = dir.list();
        if (list != null) {
            for (int i = 0, length = list.length; i < length; i++) {
                String name = list[i];
                File file = new File(dir, name);
                if (filter == null || filter.accept(file)) {
                    if (file.isDirectory()) {
                        zip(zip, file, rootPathLength, filter);
                    } else {
                        String path = file.getPath();
                        path = path.substring(rootPathLength);
                        ZipEntry entry = new ZipEntry(path.replace('\\', '/'));
                        zip.putNextEntry(entry);
                        zip.write(getBytesFromFile(file));
                        zip.closeEntry();
                    }
                }
            }
        }
    }

    private static byte[] getBytesFromFile(File f) throws IOException {
        FileInputStream fis = null;
        ByteArrayOutputStream baos = null;
        byte[] rtrn = new byte[0];
        try {
            fis = new FileInputStream(f);
            baos = new ByteArrayOutputStream();
            int b;
            while ((b = fis.read()) != -1)
                baos.write(b);
            rtrn = baos.toByteArray();
        } finally {
            if (fis != null)
                fis.close();
            if (baos != null)
                baos.close();
        }
        return rtrn;

    }
}

Related

  1. zip(String srcPath)
  2. zip(String zipFileName, Map entries)
  3. zip(String zipFileName, String inputFile)
  4. zip(String zipFileName, String[] zipEntries)
  5. zip(String zipName, String[] fileNames, boolean path)
  6. zipFile(File aFileToZip)
  7. zipFile(File file)
  8. zipFile(File file, File output)
  9. zipFile(File file, File zipFile)