Java Zip File List zipFiles(String output_dir, List files)

Here you can find the source of zipFiles(String output_dir, List files)

Description

zip Files

License

Apache License

Declaration

public static void zipFiles(String output_dir, List<File> files) 

Method Source Code

//package com.java2s;
/**/*w ww  . ja v  a2  s.  c  o  m*/
 * Copyright [2014-2016] PRAGMA, AIST, Data To Insight Center (IUB)
 *
 * 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.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFiles(String output_dir, List<File> files) {
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        FileInputStream fis = null;
        try {
            fos = new FileOutputStream(output_dir);
            zipOut = new ZipOutputStream(new BufferedOutputStream(fos));
            for (File input : files) {
                fis = new FileInputStream(input);
                ZipEntry ze = new ZipEntry(input.getName());
                zipOut.putNextEntry(ze);
                byte[] tmp = new byte[4 * 1024];
                int size = 0;
                while ((size = fis.read(tmp)) != -1) {
                    zipOut.write(tmp, 0, size);
                }
                zipOut.flush();
                fis.close();
            }
            zipOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (Exception ex) {

            }
        }
    }
}

Related

  1. ZipFiles(String destination, Vector files)
  2. zipFiles(String filename, String[] files)
  3. zipFiles(String files[], String fielPath)
  4. zipFiles(String filesPathToZip, String pathToSave)
  5. zipFiles(String output, String sDir, String sSearch)
  6. zipFiles(String source, String target)
  7. zipFiles(String srcFolder, String destZipFile)
  8. zipFiles(ZipOutputStream out, String path, File... srcFiles)
  9. zipFilesTo(Vector fileVector, String baseDir, File destFile)