Example usage for java.util.zip ZipOutputStream write

List of usage examples for java.util.zip ZipOutputStream write

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream write.

Prototype

public synchronized void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes an array of bytes to the current ZIP entry data.

Usage

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Not a directory:  " + dir);
    String[] entries = d.list();//from  www .  j  a va 2 s .  com
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }
    out.close();
}

From source file:Main.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, false);
    } else {//from   www . ja  v  a2s. co  m
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        try {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}

From source file:com.recomdata.transmart.data.export.util.ZipUtil.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*  ww  w.j a  v a 2s  .  com*/
        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

From source file:Main.java

public static void zipMutiCompress(File[] srcfile, File destFile) {
    byte[] buf = new byte[BUFFERED_SIZE];
    try {/*from www .j  av  a 2  s .c  o  m*/
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
        if (null != srcfile && srcfile.length > 0) {
            for (int i = 0; i < srcfile.length; i++) {
                File file = srcfile[i];
                if (null != file) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }
            }
        }
        out.close();
    } catch (IOException e) {

    }
}

From source file:Main.java

static private void addToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, true);
    } else {//from  w w  w .  jav a  2s  .c  o  m
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = null;
        try {
            in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + File.separator + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
}

From source file:Main.java

static void addDir(File dirObj, ZipOutputStream out) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            addDir(files[i], out);//from   w  w  w. ja  va 2 s  . c  o m
            continue;
        }
        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
        System.out.println(" Adding: " + files[i].getAbsolutePath());
        out.putNextEntry(new ZipEntry(files[i].getAbsolutePath()));
        int len;
        while ((len = in.read(tmpBuf)) > 0) {
            out.write(tmpBuf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
}

From source file:Utils.java

/**
 * Zip a list of file into one zip file.
 * /*from w  w  w . j a  v a2 s  .  c  o  m*/
 * @param files
 *          files to zip
 * @param targetZipFile
 *          target zip file
 * @throws IOException
 *           IO error exception can be thrown when copying ...
 */
public static void zipFile(final File[] files, final File targetZipFile) throws IOException {
    try {
        FileOutputStream fos = new FileOutputStream(targetZipFile);
        ZipOutputStream zos = new ZipOutputStream(fos);
        byte[] buffer = new byte[128];
        for (int i = 0; i < files.length; i++) {
            File currentFile = files[i];
            if (!currentFile.isDirectory()) {
                ZipEntry entry = new ZipEntry(currentFile.getName());
                FileInputStream fis = new FileInputStream(currentFile);
                zos.putNextEntry(entry);
                int read = 0;
                while ((read = fis.read(buffer)) != -1) {
                    zos.write(buffer, 0, read);
                }
                zos.closeEntry();
                fis.close();
            }
        }
        zos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        System.out.println("File not found : " + e);
    }

}

From source file:com.datasalt.pangool.solr.TupleSolrOutputFormat.java

private static void createZip(File dir, File out) throws IOException {
    HashSet<File> files = new HashSet<File>();
    // take only conf/ and lib/
    for (String allowedDirectory : SolrRecordWriter.getAllowedConfigDirectories()) {
        File configDir = new File(dir, allowedDirectory);
        boolean configDirExists;
        /** If the directory does not exist, and is required, bail out */
        if (!(configDirExists = configDir.exists())
                && SolrRecordWriter.isRequiredConfigDirectory(allowedDirectory)) {
            throw new IOException(String.format("required configuration directory %s is not present in %s",
                    allowedDirectory, dir));
        }/*from w ww .  ja  v  a2  s  .c o m*/
        if (!configDirExists) {
            continue;
        }
        listFiles(configDir, files); // Store the files in the existing, allowed
                                     // directory configDir, in the list of files
                                     // to store in the zip file
    }

    out.delete();
    int subst = dir.toString().length();
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(f.toString().substring(subst));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static void zipFile(String basePath, OmniFile file, ZipOutputStream zout) throws IOException {

    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipFile : " + file.getPath());

    byte[] buffer = new byte[4096];

    InputStream fin = file.getFileInputStream();

    zout.putNextEntry(new ZipEntry(basePath + file.getName()));

    int length;//from   w ww . j  av  a  2  s .  co  m

    while ((length = fin.read(buffer)) > 0) {
        zout.write(buffer, 0, length);
    }

    zout.closeEntry();
    fin.close();
}

From source file:Main.java

final static private boolean createZipFile(String out, String... in) {
    InputStream is = null;//from ww  w  . j  ava 2  s.c  o m
    ZipOutputStream zos = null;

    try {
        zos = new ZipOutputStream(new FileOutputStream(out));
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }
    try {
        for (int i = 0; i < in.length; i++) {
            is = new FileInputStream(in[i]);
            ZipEntry ze = new ZipEntry(in[i]);
            zos.putNextEntry(ze);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf)) != -1) {
                zos.write(buf, 0, len);
            }
            is.close();
            zos.closeEntry();
        }
        zos.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}