Java Zip Folder doZipFile(ZipOutputStream zipOut, File file, String dirPath)

Here you can find the source of doZipFile(ZipOutputStream zipOut, File file, String dirPath)

Description

do Zip File

License

Apache License

Declaration

private static void doZipFile(ZipOutputStream zipOut, File file, String dirPath)
            throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**/* ww w .  j av a 2s .  c om*/
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static final int BUFFER_SIZE_DIFAULT = 1024;

    private static void doZipFile(ZipOutputStream zipOut, File file, String dirPath)
            throws FileNotFoundException, IOException {
        if (file.isFile()) {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            String zipName = file.getPath().substring(dirPath.length());
            while (zipName.charAt(0) == '\\' || zipName.charAt(0) == '/') {
                zipName = zipName.substring(1);
            }
            ZipEntry entry = new ZipEntry(zipName);
            zipOut.putNextEntry(entry);
            byte[] buff = new byte[BUFFER_SIZE_DIFAULT];
            int size;
            while ((size = bis.read(buff, 0, buff.length)) != -1) {
                zipOut.write(buff, 0, size);
            }
            zipOut.closeEntry();
            bis.close();
        } else {
            File[] subFiles = file.listFiles();
            for (File subFile : subFiles) {
                doZipFile(zipOut, subFile, dirPath);
            }
        }
    }
}

Related

  1. doZip(Map zipStructure, File zipFile)
  2. doZip(Properties properties, String name, File f)
  3. doZip(String baseDir, String fileName)
  4. doZip(String filename, String zipFileName)
  5. doZip(String inFilePath, String outFilePath)
  6. zip(File rootDir, String zipPath)
  7. zip(File rootDir, String zipPath)
  8. zipDir(File dir, String classPackage, String zipFile)
  9. zipDir(File dir, String relativePath, ZipOutputStream zos)