Java Zip Directory zipSubDirectory(String basePath, File dir, ZipOutputStream zout)

Here you can find the source of zipSubDirectory(String basePath, File dir, ZipOutputStream zout)

Description

zip Sub Directory

License

Apache License

Declaration

private static void zipSubDirectory(String basePath, File dir, ZipOutputStream zout) throws IOException 

Method Source Code


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

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

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

public class Main {
    private static void zipSubDirectory(String basePath, File dir, ZipOutputStream zout) throws IOException {
        byte[] buffer = new byte[4096];
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                String path = basePath + file.getName() + "/";
                zout.putNextEntry(new ZipEntry(path));
                zipSubDirectory(path, file, zout);
                zout.closeEntry();/*from   w ww  . ja  v a2  s.  co  m*/
            } else {
                FileInputStream fin = new FileInputStream(file);
                zout.putNextEntry(new ZipEntry(basePath + file.getName()));
                int length;
                while ((length = fin.read(buffer)) > 0) {
                    zout.write(buffer, 0, length);
                }
                zout.closeEntry();
                fin.close();
            }
        }
    }
}

Related

  1. zipDirectory(String directoryName, String targetName)
  2. zipDirectory(String dirName, String zipFileName)
  3. zipDirectory(ZipOutputStream out, String stripPath, File dir, char pathSeparator)
  4. zipDirectoryEntry(ZipOutputStream output, IPath entry, long time, Set directoryEntries)
  5. zipDirectoryRecursive(File directory, String zipPath, ZipOutputStream zos, ArrayList avoidingFiles)