Java Zip Folder zipDir(String dir, ZipOutputStream zos)

Here you can find the source of zipDir(String dir, ZipOutputStream zos)

Description

zip Dir

License

MIT License

Declaration

public static void zipDir(String dir, ZipOutputStream zos) throws IOException 

Method Source Code

//package com.java2s;
/*/*from ww w  . j  a  v  a  2s.  c o  m*/
 * Copyright 2010-2011, Sikuli.org
 * Released under the MIT License.
 *
 */

import java.io.*;

import java.util.zip.*;

public class Main {
    public static void zipDir(String dir, ZipOutputStream zos) throws IOException {
        File zipDir = new File(dir);
        String[] dirList = zipDir.list();
        byte[] readBuffer = new byte[1024];
        int bytesIn;
        for (int i = 0; i < dirList.length; i++) {
            File f = new File(zipDir, dirList[i]);
            /*
             if(f.isDirectory()) {
             String filePath = f.getPath();
             zipDir(filePath, zos);
             continue;
             }
             */
            if (f.isFile()) {
                FileInputStream fis = new FileInputStream(f);
                ZipEntry anEntry = new ZipEntry(f.getName());
                zos.putNextEntry(anEntry);
                while ((bytesIn = fis.read(readBuffer)) != -1) {
                    zos.write(readBuffer, 0, bytesIn);
                }
                fis.close();
            }
        }
    }

    public static String getName(String filename) {
        File f = new File(filename);
        return f.getName();
    }
}

Related

  1. zipDir(File zipDir, ZipOutputStream zos, String name)
  2. zipDir(File zipFile, File dir, boolean includeRoot)
  3. zipDir(File zipFile, File dirObj)
  4. zipDir(final String dir2zip, final ZipOutputStream zos, final String root)
  5. zipDir(String baseDir, String dir2zip, ZipOutputStream zos)
  6. zipDir(String dir2zip, FilenameFilter filter, java.util.zip.ZipOutputStream zos, int bufferSize)
  7. zipDir(String dir2zip, String zipFileName)
  8. zipDir(String dir2zip, ZipOutputStream zipOut, String zipFileName)
  9. zipDir(String dir2zip, ZipOutputStream zos)