Java Zip Files addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)

Here you can find the source of addToZip(File f, int truncate, ZipOutputStream os, byte[] buff)

Description

Adds file or directory to the ZIP output stream.

License

LGPL

Parameter

Parameter Description
f file or directory to be added
truncate truncate name of the file
os output stream
buff buffer to use in I/O operations

Exception

Parameter Description
IOException in case of I/O error

Declaration

private static void addToZip(File f, int truncate, ZipOutputStream os, byte[] buff) throws IOException 

Method Source Code


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

import java.io.BufferedInputStream;

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  va  2  s.  com*/
     * Adds file or directory to the ZIP output stream.
     *
     * @param f file or directory to be added
     * @param truncate truncate name of the file
     * @param os output stream
     * @param buff buffer to use in I/O operations
     * @throws IOException in case of I/O error
     */
    private static void addToZip(File f, int truncate, ZipOutputStream os, byte[] buff) throws IOException {
        if (f.isFile()) {
            os.putNextEntry(new ZipEntry(f.getAbsolutePath().substring(truncate)));
            try (FileInputStream fis = new FileInputStream(f);
                    BufferedInputStream bis = new BufferedInputStream(fis)) {
                int read;
                while ((read = bis.read(buff)) != -1) {
                    os.write(buff, 0, read);
                }
            }
            os.closeEntry();
        } else {
            for (File sub : f.listFiles()) {
                addToZip(sub, truncate, os, buff);
            }
        }
    }
}

Related

  1. addFileToZip(ZipOutputStream zipOutputStream, String path, byte[] bytes)
  2. addFileToZip(ZipOutputStream zos, File file, File rootDir)
  3. addFileToZipOutputStream(File file, ZipOutputStream zipOs)
  4. addToZip(byte[] zip, String file, String fileName)
  5. addToZip(File directoryToZip, File file, ZipOutputStream zos)
  6. addToZip(File file, ZipOutputStream out, String folder)
  7. addToZip(File input, String entryName, ZipOutputStream zos, boolean withHidden, Set excludeEntries)
  8. addToZip(String path, String srcFile, ZipOutputStream zip)
  9. addToZip(String path, String srcFile, ZipOutputStream zip)