Java Zip Folder doZip(String inFilePath, String outFilePath)

Here you can find the source of doZip(String inFilePath, String outFilePath)

Description

do Zip

License

MIT License

Declaration

public static void doZip(String inFilePath, String outFilePath) 

Method Source Code


//package com.java2s;
/*//from w  ww. j  a  va 2  s. co m
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {

    public static void doZip(String inFilePath, String outFilePath) {
        try {
            File inFile = new File(inFilePath);
            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFilePath));
            zipFile(zipOut, inFile, "");
            zipOut.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException {
        if (file.isDirectory()) {
            out.putNextEntry(new ZipEntry(dir + "/"));
            dir = dir.length() == 0 ? "" : dir + "/";

            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                zipFile(out, files[i], dir + files[i].getName());
            }
        } else {
            FileInputStream fis = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(dir));

            int tempByte;
            byte[] buffer = new byte[1024];
            while ((tempByte = fis.read(buffer)) > 0) {
                out.write(buffer, 0, tempByte);
            }

            fis.close();
        }
    }
}

Related

  1. doZip(File fileIn, String fileOut)
  2. doZip(Map zipStructure, File zipFile)
  3. doZip(Properties properties, String name, File f)
  4. doZip(String baseDir, String fileName)
  5. doZip(String filename, String zipFileName)
  6. doZipFile(ZipOutputStream zipOut, File file, String dirPath)
  7. zip(File rootDir, String zipPath)
  8. zip(File rootDir, String zipPath)
  9. zipDir(File dir, String classPackage, String zipFile)