Java Zip Files makeZip(File dir, File zipFile)

Here you can find the source of makeZip(File dir, File zipFile)

Description

make Zip

License

Apache License

Declaration

public static void makeZip(File dir, File zipFile) throws IOException, FileNotFoundException 

Method Source Code


//package com.java2s;
/*// w  w  w  .  java 2s.  c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;

public class Main {
    private static byte buf[] = new byte[256];
    private static int len;

    public static void makeZip(File dir, File zipFile) throws IOException, FileNotFoundException {
        JarOutputStream jos = new JarOutputStream(new FileOutputStream(zipFile));
        String as[] = dir.list();
        if (as != null) {
            for (int i = 0; i < as.length; i++)
                recurseFiles(jos, new File(dir, as[i]), "");
        }
        jos.close();
    }

    private static void recurseFiles(JarOutputStream jos, File file, String s)
            throws IOException, FileNotFoundException {

        if (file.isDirectory()) {
            s = s + file.getName() + "/";
            jos.putNextEntry(new JarEntry(s));
            String as[] = file.list();
            if (as != null) {
                for (int i = 0; i < as.length; i++)
                    recurseFiles(jos, new File(file, as[i]), s);
            }
        } else {
            if (file.getName().endsWith("MANIFEST.MF") || file.getName().endsWith("META-INF/MANIFEST.MF")) {
                return;
            }
            JarEntry jarentry = new JarEntry(s + file.getName());
            FileInputStream fileinputstream = new FileInputStream(file);
            BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
            jos.putNextEntry(jarentry);
            while ((len = bufferedinputstream.read(buf)) >= 0) {
                jos.write(buf, 0, len);
            }
            bufferedinputstream.close();
            jos.closeEntry();
        }
    }
}

Related

  1. addToZip(ZipOutputStream zos, String rootDirectoryName, String fileName)
  2. addToZipFile(InputStream source, String entryName, ZipOutputStream zos)
  3. addToZipFile(String fileName, ZipOutputStream zos)
  4. fileToZip(File file, File zipFile)
  5. fileToZipFile(File toZip, File output)
  6. makeZip(String[] inFilePaths, String zipFilePath)
  7. makeZipBinary(Map instance, String fileNameSeperator)