Java Zip File zip(String path, String zipFilePath)

Here you can find the source of zip(String path, String zipFilePath)

Description

zip

License

Open Source License

Declaration

public static void zip(String path, String zipFilePath) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zip(String path, String zipFilePath) throws IOException {
        FileOutputStream fos = new FileOutputStream(zipFilePath);
        ZipOutputStream zos = new ZipOutputStream(fos);

        File file = new File(path);

        putFileToZipEntry(zos, file, file.getName());

        zos.close();/*from   ww w.java  2s. c  o  m*/
    }

    private static void putFileToZipEntry(ZipOutputStream zos, File file, String path) throws IOException {
        //File file = new File(path);
        if (file.isFile()) {
            ZipEntry entry = new ZipEntry(path);
            zos.putNextEntry(entry);
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[4096];
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            fis.close();
            zos.closeEntry();
        } else if (file.isDirectory()) {
            File[] subFiles = file.listFiles();

            //String[] subFiles = file.list();
            for (File subFile : subFiles) {
                putFileToZipEntry(zos, subFile, path + File.separator + subFile.getName());
            }
        }

        //ignore other kind of files
    }
}

Related

  1. zip(String dir, String destFile)
  2. zip(String filesDirToBeZipped, String destFileName, String manifest)
  3. zip(String inputStr)
  4. zip(String outputFileName, String inputFileName)
  5. Zip(String path, File file)
  6. zip(String payload)
  7. zip(String src, String des)
  8. zip(String src, String dest, PrintStream stream)
  9. zip(String srcPath)