Java Zip File zip(File inputDirectory, File zippedFile, FileFilter filter)

Here you can find the source of zip(File inputDirectory, File zippedFile, FileFilter filter)

Description

zip

License

Apache License

Declaration

public static void zip(File inputDirectory, File zippedFile, FileFilter filter) 

Method Source Code


//package com.java2s;
/*//from w w w  .ja  v a  2s .c  o  m
 * Copyright 2011 cruxframework.org.
 * 
 * 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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zip(File inputDirectory, File zippedFile, FileFilter filter) {
        int BUFFER = 2048;
        try {
            BufferedInputStream origin = null;
            FileOutputStream dest = new FileOutputStream(zippedFile);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
            // out.setMethod(ZipOutputStream.DEFLATED);
            byte data[] = new byte[BUFFER];
            // get a list of files from current directory
            File files[];

            if (filter != null) {
                files = inputDirectory.listFiles(filter);
            } else {
                files = inputDirectory.listFiles();
            }
            for (int i = 0; i < files.length; i++) {
                FileInputStream fi = new FileInputStream(files[i]);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(files[i].getName());
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * @param file
     * @return
     * @throws IOException
     */
    public static String read(File file) throws IOException {
        return read(new FileInputStream(file));
    }

    /**
     * @param in
     * @return
     * @throws IOException
     */
    public static String read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.flush();
        out.close();

        return new String(out.toByteArray());
    }

    /**
     * @param text
     * @param f
     * @throws IOException
     */
    public static void write(String text, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);
        out.write(text.getBytes());
        out.close();
    }

    /**
     * @param in
     * @param f
     * @throws IOException
     */
    public static void write(InputStream in, File f) throws IOException {
        FileOutputStream out = new FileOutputStream(f);

        int read = 0;
        byte[] buff = new byte[1024];
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }
        in.close();
        out.close();
    }
}

Related

  1. zip(File file, File zip)
  2. zip(File file, String dest)
  3. zip(File fromFile, File toFile)
  4. zip(File input, File output)
  5. zip(File input, File outputZip)
  6. zip(File path)
  7. zip(File source, File target)
  8. zip(File source, File target)
  9. zip(File sourceDir, OutputStream targetStream)