Java Zip Directory addDirToArchive(ZipOutputStream zos, File srcFile)

Here you can find the source of addDirToArchive(ZipOutputStream zos, File srcFile)

Description

Adds file / folder to zip output stream.

License

LGPL

Parameter

Parameter Description
zos a parameter
srcFile a parameter

Declaration

public static void addDirToArchive(ZipOutputStream zos, File srcFile) 

Method Source Code

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

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 {
    /**/*from   w ww. j  a v a  2s  .  c om*/
     * Adds file / folder to zip output stream. Method works recursively.
     * @param zos
     * @param srcFile 
     */
    public static void addDirToArchive(ZipOutputStream zos, File srcFile) {
        File[] files = srcFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            // if the file is directory, use recursion
            if (files[i].isDirectory()) {
                addDirToArchive(zos, files[i]);
                continue;
            }
            try {
                // create byte buffer
                byte[] buffer = new byte[1024];
                FileInputStream fis = new FileInputStream(files[i]);
                zos.putNextEntry(new ZipEntry(files[i].getName()));
                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
                // close the InputStream
                fis.close();
            } catch (IOException ioe) {
                System.out.println("IOException :" + ioe);
            }
        }
    }
}

Related

  1. addDirectoryToZip(File directory, File base, String dirPrefix, ZipOutputStream zos)
  2. addDirectoryToZip(ZipOutputStream out, File dir, String prefix)
  3. addDirectoryToZip(ZipOutputStream zipOutputStream, File dirToZip, String basePath, File fileToExclude)
  4. addDirectoryToZip(ZipOutputStream zipOutputStream, String dirName)
  5. addDirToArchive(ZipOutputStream zop, File srcFile)
  6. addDirToArchive(ZipOutputStream zos, String path, File initialDir)
  7. zip(File dir, File base, ZipOutputStream out)
  8. zip(File dir, ZipOutputStream out, String prefix)
  9. zip(File dir2zip, File zipFile)