Java Zip File List zipFilesTo(Vector fileVector, String baseDir, File destFile)

Here you can find the source of zipFilesTo(Vector fileVector, String baseDir, File destFile)

Description

Zips all files in "fileVector" to the zipfile "destFile".

License

Open Source License

Parameter

Parameter Description
fileVector Files to be zipped
baseDir Root directory for this zip file
destFile Destination file

Declaration

public static void zipFilesTo(Vector<File> fileVector, String baseDir, File destFile) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Iterator;
import java.util.Vector;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**//from w  ww.  jav  a  2s.c o  m
     * Zips all files in "fileVector" to the zipfile "destFile".
     * The pathnames of all files in fileVector must start with baseDir!
     * @param fileVector Files to be zipped
     * @param baseDir Root directory for this zip file
     * @param destFile Destination file
     */
    public static void zipFilesTo(Vector<File> fileVector, String baseDir, File destFile) {
        FileOutputStream ops = null;
        ZipOutputStream zos = null;
        int basedirlen = baseDir.length();
        if (!baseDir.endsWith(File.separator))
            basedirlen++;
        try {
            ops = new FileOutputStream(destFile);
            zos = new ZipOutputStream(ops);

            Iterator<File> iter = fileVector.iterator();
            while (iter.hasNext()) {
                File file = iter.next();
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    String name = file.getPath().substring(basedirlen);
                    name = name.replace('\\', '/'); // Zip uses '/' as separator
                    ZipEntry zi = new ZipEntry(name);
                    zos.putNextEntry(zi);
                    copystream(fis, zos);
                    zos.closeEntry();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fis != null)
                            fis.close();
                    } catch (Exception e) {
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (zos != null)
                    zos.close();
                else if (ops != null)
                    ops.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Copies the input stream to the output stream using a 1 kB buffer
     * @throws IOException
     */
    private static void copystream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);
    }
}

Related

  1. zipFiles(String output, String sDir, String sSearch)
  2. zipFiles(String output_dir, List files)
  3. zipFiles(String source, String target)
  4. zipFiles(String srcFolder, String destZipFile)
  5. zipFiles(ZipOutputStream out, String path, File... srcFiles)