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.microsoft.azurebatch.jenkins.utils.ZipHelper.java

private static void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean isEmptyFolder)
        throws IOException {
    File folder = new File(srcFile);

    if (isEmptyFolder == true) {
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/"));
    } else {/*from w ww. j ava  2  s.c  om*/
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            byte[] buf = new byte[1024];
            int len;
            try (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:com.taobao.android.builder.tools.zip.ZipUtils.java

private static void write(InputStream inputStream, ZipOutputStream out, byte[] buffer) throws IOException {
    int length = inputStream.read(buffer);
    while (length != -1) {
        out.write(buffer, 0, length);
        length = inputStream.read(buffer);
    }//from  ww  w . ja va 2  s.  c  o m
    closeQuitely(inputStream);
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input file into a ZIP file container.
 *
 * @param aInFileName The file name to be compressed.
 * @param aZipFileName The file name of the ZIP container.
 * @throws IOException Related to opening the file streams and
 * related read/write operations.//w w  w.j  a  v a  2 s. c om
 */
static public void zipFile(String aInFileName, String aZipFileName) throws IOException {
    File inFile;
    int byteCount;
    byte[] ioBuf;
    FileInputStream fileIn;
    ZipOutputStream zipOut;
    FileOutputStream fileOut;

    inFile = new File(aInFileName);
    if (inFile.isDirectory())
        return;

    ioBuf = new byte[FILE_IO_BUFFER_SIZE];
    fileIn = new FileInputStream(inFile);
    fileOut = new FileOutputStream(aZipFileName);
    zipOut = new ZipOutputStream(fileOut);
    zipOut.putNextEntry(new ZipEntry(inFile.getName()));
    byteCount = fileIn.read(ioBuf);
    while (byteCount > 0) {
        zipOut.write(ioBuf, 0, byteCount);
        byteCount = fileIn.read(ioBuf);
    }
    fileIn.close();
    zipOut.closeEntry();
    zipOut.close();
}

From source file:apim.restful.exportimport.utils.ArchiveGenerator.java

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos)
        throws FileNotFoundException, IOException {

    FileInputStream fis = new FileInputStream(file);

    // we want the zipEntry's path to be a relative path that is relative
    // to the directory being zipped, so chop off the rest of the path
    String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
            file.getCanonicalPath().length());
    System.out.println("Writing '" + zipFilePath + "' to zip file");
    ZipEntry zipEntry = new ZipEntry(zipFilePath);
    zos.putNextEntry(zipEntry);//from   ww  w .j  a v  a  2 s .  co  m

    byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

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

From source file:Main.java

private static boolean addEntryInputStream(ZipOutputStream zos, String entryName, InputStream inputStream) {
    final ZipEntry zipEntry = new ZipEntry(entryName);
    try {/*from  w  ww  .  j  a v a  2  s. c o m*/
        zos.putNextEntry(zipEntry);
    } catch (final ZipException e) {

        // Ignore duplicate entry - no overwriting
        return false;
    } catch (final IOException e) {
        throw new RuntimeException("Error while adding zip entry " + zipEntry, e);
    }
    final int buffer = 2048;
    final BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, buffer);
    int count;
    try {
        final byte data[] = new byte[buffer];
        while ((count = bufferedInputStream.read(data, 0, buffer)) != -1) {
            zos.write(data, 0, count);
        }
        bufferedInputStream.close();
        inputStream.close();
        zos.closeEntry();
        return true;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.emf.emfstore.common.model.util.FileUtil.java

private static void zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
        throws IOException {
    if (current.isDirectory()) {
        for (File file : current.listFiles()) {
            if (!".".equals(file.getName()) && !"..".equals(file.getName())) {
                zip(file, rootPath, zipStream, buffer);
            }//ww w .  ja  v a2  s .  com
        }
    } else if (current.isFile()) {
        zipStream.putNextEntry(new ZipEntry(current.getPath().replace(rootPath, "")));
        FileInputStream file = new FileInputStream(current);
        int read;
        while ((read = file.read(buffer)) != -1) {
            zipStream.write(buffer, 0, read);
        }
        zipStream.closeEntry();
        file.close();
    } else {
        throw new IllegalStateException();
    }
}

From source file:au.org.ands.vocabs.toolkit.utils.ToolkitFileUtils.java

/** Add a file to a ZIP archive.
 * @param zos The ZipOutputStream representing the ZIP archive.
 * @param file The File which is to be added to the ZIP archive.
 * @return True if adding succeeded./*from w  w w. ja  v  a  2 s . com*/
 * @throws IOException Any exception when reading/writing data.
 */
private static boolean zipFile(final ZipOutputStream zos, final File file) throws IOException {
    if (!file.canRead()) {
        logger.error("zipFile can not read " + file.getCanonicalPath());
        return false;
    }
    zos.putNextEntry(new ZipEntry(file.getName()));
    FileInputStream fis = new FileInputStream(file);

    byte[] buffer = new byte[BUFFER_SIZE];
    int byteCount = 0;
    while ((byteCount = fis.read(buffer)) != -1) {
        zos.write(buffer, 0, byteCount);
    }
    fis.close();
    zos.closeEntry();
    return true;
}

From source file:Main.java

private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException {
    if (srcFile.isDirectory()) {
        File subfile[] = srcFile.listFiles();
        for (int i = 0; i < subfile.length; i++) {
            compressDir(subfile[i], out, destPath);
        }//from   w ww  . j  av a 2 s .c  o  m
    } else {
        InputStream in = new FileInputStream(srcFile);
        String name = srcFile.getAbsolutePath().replace(destPath, "");
        if (name.startsWith("\\"))
            name = name.substring(1);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(srcFile.length());
        entry.setTime(srcFile.lastModified());
        out.putNextEntry(entry);
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);

        out.closeEntry();
        in.close();
    }
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Adds the file to zip.//from  w  w w.j  ava  2  s.  co m
 * @param path the path
 * @param srcFile the src file
 * @param zip the zip
 */
private static void addFileToZip(final String path, final String srcFile, final ZipOutputStream zip) {

    try {
        final File folder = new File(srcFile);
        if (folder.isDirectory()) {
            addFolderToZip(path, srcFile, zip);
        } else {
            final byte[] buf = new byte[1024];
            int len;
            final FileInputStream inputStream = new FileInputStream(srcFile);
            zip.putNextEntry(
                    new ZipEntry(new StringBuilder(path).append(SLASH).append(folder.getName()).toString()));
            do {
                len = inputStream.read(buf);
                zip.write(buf, 0, len);
            } while (len > 0);
            inputStream.close();
        }
    } catch (IOException e) {
        LOGGER.error("Error in add flies to zip", e);
    }
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress multiple Files./*from ww w .  j a va 2s. c om*/
 * 
 * @param logFiles
 *            files to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File[] logFiles) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {

        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        for (File each : logFiles) {
            try {
                fio = new FileInputStream(each);
                ZipEntry zipEntry = new ZipEntry(each.getName());
                zipEntry.setTime(each.lastModified());
                zos.putNextEntry(zipEntry);
                byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
                int count = 0;
                while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
                    zos.write(buffer, 0, count);
                }
                zos.closeEntry();
            } catch (IOException e) {
                LOGGER.error("Error occurs while compress {}", each.getAbsolutePath());
                LOGGER.error("Details", e);
            } finally {
                IOUtils.closeQuietly(fio);
            }
        }
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.info("Error occurs while compress log : {} ", e.getMessage());
        LOGGER.debug("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}