Java Zip File zip(String src, String dest, PrintStream stream)

Here you can find the source of zip(String src, String dest, PrintStream stream)

Description

zip

License

Apache License

Declaration

public static void zip(String src, String dest, PrintStream stream)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BYTE_ARRAY_SIZE = 65536;

    public static void zip(String src, String dest, PrintStream stream)
            throws IOException {
        src = src.replace("~", System.getProperty("user.home"));
        dest = dest.replace("~", System.getProperty("user.home"));
        File srcFile = new File(src);
        if (!srcFile.exists()) {
            throw new RuntimeException(src + " does not exist.");
        }//from w  w w . j  av  a2  s  .  c o m
        ZipOutputStream zos = new ZipOutputStream(new CheckedOutputStream(
                new FileOutputStream(dest), new CRC32()));
        innerZip(zos, srcFile, null, stream);
        zos.flush();
        zos.close();
    }

    private static void innerZip(ZipOutputStream zos, File srcFile,
            String parentPath, PrintStream stream) throws IOException {
        if (srcFile == null || !srcFile.exists()) {
            return;
        }
        String currentPath = srcFile.getName();
        if (parentPath != null && parentPath.length() != 0) {
            currentPath = parentPath + File.separator + currentPath;
        }
        if (stream != null) {
            stream.println(currentPath);
        }
        if (srcFile.isDirectory()) {
            // zip folder
            int fileCount = 0;
            // list all files and recurse into sub-folders
            for (File file : srcFile.listFiles()) {
                fileCount++;
                innerZip(zos, file, currentPath, stream);
            }
            // create empty folder entry if this is an empty folder
            if (fileCount == 0) {
                zos.putNextEntry(new ZipEntry(currentPath + File.separator));
                zos.closeEntry();
            }
        } else {
            // zip file
            FileInputStream fis = new FileInputStream(srcFile);
            byte[] buffer = new byte[BYTE_ARRAY_SIZE];
            int read;
            zos.putNextEntry(new ZipEntry(currentPath));
            while ((read = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, read);
            }
            zos.closeEntry();
            fis.close();
        }
    }
}

Related

  1. zip(String outputFileName, String inputFileName)
  2. Zip(String path, File file)
  3. zip(String path, String zipFilePath)
  4. zip(String payload)
  5. zip(String src, String des)
  6. zip(String srcPath)
  7. zip(String zipFileName, Map entries)
  8. zip(String zipFileName, String inputFile)
  9. zip(String zipFileName, String[] zipEntries)