Java Zip File List zipFiles(Collection resFileList, File zipFile)

Here you can find the source of zipFiles(Collection resFileList, File zipFile)

Description

zip Files

License

Apache License

Declaration

public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  ww w  .ja  v a  2 s.  c om*/
 * Copyright (c) 2014,KJFrameForAndroid Open Source Project,??.
 *
 * Licensed 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.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Collection;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFF_SIZE = 1024 * 1024;

    public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.close();
    }

    public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.setComment(comment);
        zipout.close();
    }

    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath)
            throws FileNotFoundException, IOException {
        rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
        rootpath = new String(rootpath.getBytes("8859_1"), "GB2312");
        if (resFile.isDirectory()) {
            File[] fileList = resFile.listFiles();
            for (File file : fileList) {
                zipFile(file, zipout, rootpath);
            }
        } else {
            byte buffer[] = new byte[BUFF_SIZE];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
            zipout.putNextEntry(new ZipEntry(rootpath));
            int realLength;
            while ((realLength = in.read(buffer)) != -1) {
                zipout.write(buffer, 0, realLength);
            }
            in.close();
            zipout.flush();
            zipout.closeEntry();
        }
    }
}

Related

  1. zip(String[] sFl, String sZip)
  2. zip(String[] sourceFiles, String zipFile, String directory)
  3. zip(String[] srcFiles, String dstFile, String comment)
  4. zipFileList(List files, File destinationDir)
  5. zipFiles(ArrayList files, String destZipFile)
  6. zipFiles(File file, JarOutputStream jos, String pathName)
  7. zipFiles(File output, File root, List fileList)
  8. zipFiles(File rootDir, File currDir, ZipOutputStream zos)
  9. zipFiles(File[] dirList, ZipOutputStream zos)