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:com.edgenius.core.util.ZipFileUtil.java

private static void addEntry(ZipOutputStream zop, File entry, String entryName) throws IOException {
    if (StringUtils.isBlank(entryName))
        return;//from w w  w .j a va  2 s . c  o  m

    BufferedInputStream source = null;
    if (entry != null) {
        source = new BufferedInputStream(new FileInputStream(entry));
    } else {
        //to make this as directory
        if (!entryName.endsWith("/"))
            entryName += "/";
    }
    ZipEntry zipEntry = new ZipEntry(entryName);
    zop.putNextEntry(zipEntry);

    if (entry != null) {
        //transfer bytes from file to ZIP file
        byte[] data = new byte[BUFFER_SIZE];
        int length;
        while ((length = source.read(data)) > 0) {
            zop.write(data, 0, length);
        }
        IOUtils.closeQuietly(source);
    }
    zop.closeEntry();

}

From source file:Main.java

private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws IOException {

    java.io.File files = new java.io.File(sourcePath);
    java.io.File[] fileList = files.listFiles();

    String entryPath = "";
    BufferedInputStream input = null;
    for (java.io.File file : fileList) {
        if (file.isDirectory()) {
            zipFile(zipOutputStream, file.getPath());
        } else {/*from w w w.  j a  v  a 2s. c  o m*/
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fileInputStream = new FileInputStream(file.getPath());
            input = new BufferedInputStream(fileInputStream, BUFFER_SIZE);
            entryPath = file.getAbsolutePath().replace(parentPath, "");

            ZipEntry entry = new ZipEntry(entryPath);
            zipOutputStream.putNextEntry(entry);

            int count;
            while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
                zipOutputStream.write(data, 0, count);
            }
            input.close();
        }
    }

}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

private static void zipDirectory(File directory, String name, ZipOutputStream zos) throws IOException {
    // *MUST* append the trailing slash for a ZipEntry to identify an
    // entry as a directory.
    name += "/";//  ww w. j a  va2s  .com

    zos.putNextEntry(new ZipEntry(name));
    zos.closeEntry();

    String[] entryList = directory.list();

    for (int i = 0; i < entryList.length; ++i) {
        File f = new File(directory, entryList[i]);

        if (f.isDirectory()) {
            zipDirectory(f, name + f.getName(), zos);
        } else {
            FileInputStream fis = new FileInputStream(f);
            ZipEntry entry = new ZipEntry(name + f.getName());
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesIn = 0;

            zos.putNextEntry(entry);

            while ((bytesIn = fis.read(buffer)) != -1) {
                zos.write(buffer, 0, bytesIn);
            }

            fis.close();
            zos.closeEntry();
        }
    }
}

From source file:com.ibm.amc.FileManager.java

private static void compressFile(File file, ZipOutputStream out, String basedir) {
    try {/*from  ww w . ja v  a  2 s . c o m*/
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        ZipEntry entry = new ZipEntry(basedir + file.getName());
        out.putNextEntry(entry);
        int length = Long.valueOf(file.length()).intValue();
        int buffer = ZIP_BUFFER;
        if (length != 0) {
            buffer = length;
        }
        int count;
        byte data[] = new byte[buffer];
        while ((count = bis.read(data, 0, buffer)) != -1) {
            out.write(data, 0, count);
        }
        bis.close();
    } catch (Exception e) {
        throw new AmcRuntimeException(e);
    }
}

From source file:com.mvdb.etl.actions.ActionUtils.java

private static void zipDir(String origDir, File dirObj, ZipOutputStream zos) throws IOException {
    File[] files = dirObj.listFiles();
    byte[] tmpBuf = new byte[1024];

    for (int i = 0; i < files.length; i++) {
        if (files[i].isDirectory()) {
            zipDir(origDir, files[i], zos);
            continue;
        }/*from w w w  . java  2 s. c  o  m*/
        String wAbsolutePath = files[i].getAbsolutePath().substring(origDir.length() + 1,
                files[i].getAbsolutePath().length());
        FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
        zos.putNextEntry(new ZipEntry(wAbsolutePath));
        int len;
        while ((len = in.read(tmpBuf)) > 0) {
            zos.write(tmpBuf, 0, len);
        }
        zos.closeEntry();
        in.close();
    }
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void addFileToZip(File file, ZipOutputStream zos) throws Exception {

    zos.putNextEntry(new ZipEntry(file.getName()));

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

    long bytesRead = 0;
    byte[] bytesIn = new byte[1024];
    int read = 0;

    while ((read = bis.read(bytesIn)) != -1) {
        zos.write(bytesIn, 0, read);
        bytesRead += read;/*from  www  .  j  av  a 2 s  .c o m*/
    }

    zos.closeEntry();
    bis.close();
}

From source file:Main.java

/**
 * Zips a subfolder/* ww  w.j  a  va 2s.c  o  m*/
 */
protected static void zipSubFolder(ZipOutputStream zipOut, File srcFolder, int basePathLength)
        throws IOException {
    final int BUFFER = 2048;

    File[] fileList = srcFolder.listFiles();
    BufferedInputStream bis = null;

    for (File file : fileList) {

        if (file.isDirectory()) {
            zipSubFolder(zipOut, file, basePathLength);

        } else {
            byte data[] = new byte[BUFFER];
            String unmodifiedFilePath = file.getPath();
            String relativePath = unmodifiedFilePath.substring(basePathLength);
            Log.d("ZIP SUBFOLDER", "Relative Path : " + relativePath);

            FileInputStream fis = new FileInputStream(unmodifiedFilePath);
            bis = new BufferedInputStream(fis, BUFFER);

            ZipEntry zipEntry = new ZipEntry(relativePath);
            zipOut.putNextEntry(zipEntry);

            int count;
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                zipOut.write(data, 0, count);
            }

            bis.close();
        }
    }
}

From source file:net.vhati.modmanager.cli.SlipstreamCLI.java

private static void addDirToArchive(ZipOutputStream zos, File dir, String pathPrefix) throws IOException {
    if (pathPrefix == null)
        pathPrefix = "";

    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            addDirToArchive(zos, f, pathPrefix + f.getName() + "/");
            continue;
        }/*w w  w.j a  v  a  2 s.  c o  m*/

        FileInputStream is = null;
        try {
            is = new FileInputStream(f);
            zos.putNextEntry(new ZipEntry(pathPrefix + f.getName()));

            byte[] buf = new byte[4096];
            int len;
            while ((len = is.read(buf)) >= 0) {
                zos.write(buf, 0, len);
            }

            zos.closeEntry();
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.eclipse.tracecompass.integration.swtbot.tests.projectexplorer.TestDirectoryStructureUtil.java

private static void addToArchive(ZipOutputStream zos, File dir, File root) throws IOException {
    byte[] buffer = new byte[1024];
    int length;/*  www  .  j av  a  2  s  . c  o  m*/
    int index = root.getAbsolutePath().length();
    for (File file : dir.listFiles()) {
        String name = file.getAbsolutePath().substring(index);
        if (file.isDirectory()) {
            if (file.listFiles().length != 0) {
                addToArchive(zos, file, root);
            } else {
                zos.putNextEntry(new ZipEntry(name + File.separator));
                zos.closeEntry();
            }
        } else {
            try (FileInputStream fis = new FileInputStream(file)) {
                zos.putNextEntry(new ZipEntry(name));
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }
                zos.closeEntry();
            }
        }
    }
}

From source file:net.semanticmetadata.lire.utils.FileUtils.java

public static void zipDirectory(File directory, File base, ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zipDirectory(files[i], base, zos);
        } else {//from   w  w  w. j  a v a 2s.c om
            FileInputStream in = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
            zos.putNextEntry(entry);
            while (-1 != (read = in.read(buffer))) {
                zos.write(buffer, 0, read);
            }
            in.close();
        }
    }
}