Java Zip File zip(File fromFile, File toFile)

Here you can find the source of zip(File fromFile, File toFile)

Description

Zip the sourceFile into toFile.

License

Open Source License

Parameter

Parameter Description
sourceFile a parameter
toFile a parameter

Exception

Parameter Description

Declaration

public static void zip(File fromFile, File toFile) throws IOException 

Method Source Code


//package com.java2s;
/*// www. j a va  2s. c  om
Milyn - Copyright (C) 2006
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software
Foundation.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
See the GNU Lesser General Public License for more details:
http://www.gnu.org/licenses/lgpl.txt
 */

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;
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 {
    /**
     * Zip the <tt>sourceFile</tt> into <tt>toFile</tt>.
     *
     * @param sourceFile
     * @param toFile
     * @throws java.io.IOException
     */
    public static void zip(File fromFile, File toFile) throws IOException {
        zip(new FileInputStream(fromFile), toFile);
    }

    /**
     * zip the contents of an {@link InputStream}. Note that once the
     * zip file has been created, the {@link InputStream} will be closed.
     * @param is
     * @param toFile
     * @throws IOException
     */
    public static void zip(InputStream is, File toFile) throws IOException {
        zip(is, toFile, true);
    }

    /**
     * zip the contents of an {@link InputStream}. Note that once the
     * zip file has been created, the {@link InputStream} will be closed.
     * @param is
     * @param toFile
     * @throws IOException
     */
    public static void zip(InputStream is, File toFile, boolean closeInputStream) throws IOException {
        final int BUFFER_SIZE = 2048;
        BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE);
        try {
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(toFile)));
            try {
                // just the name instead of full path + name
                ZipEntry entry = new ZipEntry(toFile.getName());
                out.putNextEntry(entry);
                byte data[] = new byte[BUFFER_SIZE];
                int count;
                while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                out.close();
            }
        } finally {
            if (closeInputStream) {
                in.close();
            }
        }
    }
}

Related

  1. zip(File f, File destfile)
  2. zip(File f, File out)
  3. zip(File file, File output)
  4. zip(File file, File zip)
  5. zip(File file, String dest)
  6. zip(File input, File output)
  7. zip(File input, File outputZip)
  8. zip(File inputDirectory, File zippedFile, FileFilter filter)
  9. zip(File path)