Java Zip File List zip(List fileNames, String outFileName)

Here you can find the source of zip(List fileNames, String outFileName)

Description

zip

License

Apache License

Declaration

public static void zip(List<String> fileNames, String outFileName) 

Method Source Code


//package com.java2s;
/*//from w w  w  .java2  s .co  m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;

import java.util.List;

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

public class Main {
    public static void zip(List<String> fileNames, String outFileName) {
        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // Create the ZIP file
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName));

            // Compress the files
            for (String fileName : fileNames) {
                FileInputStream in = new FileInputStream(fileName);

                // Add ZIP entry to output stream.
                out.putNextEntry(new ZipEntry(fileName));

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

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

            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void zip(List<String> fileNames, String outFileName, String withoutBase) {
        // Create a buffer for reading the files
        byte[] buf = new byte[1024];

        try {
            // Create the ZIP file
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName));

            // Compress the files
            for (String fileName : fileNames) {
                FileInputStream in = new FileInputStream(fileName);

                // Add ZIP entry to output stream.
                if (withoutBase != null) {
                    fileName = fileName.substring(withoutBase.length() + 1);
                }
                out.putNextEntry(new ZipEntry(fileName));

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

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

            // Complete the ZIP file
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Related

  1. zip(File zipFile, File[] files)
  2. zip(File zipFile, List files)
  3. zip(Iterable files, String baseFolderName, String toZipFile)
  4. zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)
  5. zip(List fileNames, List fileContents)
  6. zip(List srcFiles, OutputStream os)
  7. zip(String[] filename, String outname)
  8. zip(String[] sFl, String sZip)
  9. zip(String[] sourceFiles, String zipFile, String directory)