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:io.sledge.core.impl.installer.SledgePackageConfigurer.java

private void addToZipFile(String zipEntryName, InputStream inStream, ZipOutputStream zipStream)
        throws IOException {
    log.debug("Adding zip entry: " + zipEntryName);

    ZipEntry entry = new ZipEntry(zipEntryName);
    zipStream.putNextEntry(entry);//ww w  . ja  v a 2  s .  co m

    byte[] readBuffer = new byte[2048];
    int amountRead;
    int written = 0;

    while ((amountRead = inStream.read(readBuffer)) > 0) {
        zipStream.write(readBuffer, 0, amountRead);
        written += amountRead;
    }

    zipStream.closeEntry();
    log.debug("Written " + written + " bytes for: " + zipEntryName);
}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createContentType(ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("[Content_Types].xml");
    zos.putNextEntry(ze);/*  w  ww.  ja  va 2s.  c om*/

    InputStream is = getClass().getResourceAsStream("[Content_Types].xml");
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

    is.close();
    zos.closeEntry();

}

From source file:org.carlspring.strongbox.artifact.generator.NugetPackageGenerator.java

private void createRels(String id, ZipOutputStream zos) throws IOException {
    ZipEntry ze = new ZipEntry("_rels/.rels");
    zos.putNextEntry(ze);//from  ww  w  .j  a  va2s  .  co  m

    ByteArrayInputStream is = new ByteArrayInputStream(String.format(RELS_CONTENT, id).getBytes());
    byte[] buffer = new byte[4096];
    int len;
    while ((len = is.read(buffer)) > 0) {
        zos.write(buffer, 0, len);
    }

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

From source file:ZipImploder.java

protected void copyFileEntry(ZipOutputStream zos, DataInputStream dis) throws IOException {
    byte[] bytes = readAllBytes(dis);
    zos.write(bytes, 0, bytes.length);
}

From source file:org.apache.parquet.tools.submit.FileFormat.java

private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {//from   www . j  a  v a  2s. c o  m
        byte[] buf = new byte[1024];
        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:com.taobao.android.builder.tools.zip.ZipUtils.java

/**
 * zip//from   w  w w .j a v a 2  s  . c  o m
 *
 * @param zipFile
 * @param file
 * @param destPath
 * @param overwrite ?
 * @throws java.io.IOException
 */
public static void addFileToZipFile(File zipFile, File outZipFile, File file, String destPath,
        boolean overwrite) throws IOException {
    byte[] buf = new byte[1024];
    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outZipFile));
    ZipEntry entry = zin.getNextEntry();
    boolean addFile = true;
    while (entry != null) {
        boolean addEntry = true;
        String name = entry.getName();
        if (StringUtils.equalsIgnoreCase(name, destPath)) {
            if (overwrite) {
                addEntry = false;
            } else {
                addFile = false;
            }
        }
        if (addEntry) {
            ZipEntry zipEntry = null;
            if (STORED == entry.getMethod()) {
                zipEntry = new ZipEntry(entry);
            } else {
                zipEntry = new ZipEntry(name);
            }
            out.putNextEntry(zipEntry);
            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }

    if (addFile) {
        InputStream in = new FileInputStream(file);
        // Add ZIP entry to output stream.
        ZipEntry zipEntry = new ZipEntry(destPath);
        out.putNextEntry(zipEntry);
        // Transfer bytes from the file to the ZIP file
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        // Complete the entry
        out.closeEntry();
        in.close();
    }
    // Close the streams
    zin.close();
    out.close();
}

From source file:it.polimi.diceH2020.launcher.utility.FileUtility.java

private void addFileToZip(@NotNull File path, @NotNull File srcFile, @NotNull ZipOutputStream zip)
        throws IOException {
    if (srcFile.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*  w w w .  j av  a2 s. co  m*/
        byte[] buf = new byte[1024];
        int len;
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(srcFile))) {
            zip.putNextEntry(makeEntry(path, srcFile));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        }
    }
}

From source file:de.uniwue.info6.database.jaxb.ScenarioExporter.java

/**
 *
 *
 * @param files// w w  w.j  a va  2s. co m
 * @param zipfile
 * @return
 */
private File zip(List<File> files, File zipfile) {
    byte[] buf = new byte[1024];
    try {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        for (int i = 0; i < files.size(); i++) {
            FileInputStream in = new FileInputStream(files.get(i));
            out.putNextEntry(new ZipEntry(files.get(i).getName()));
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }
        out.close();
        for (File file : files) {
            if (file.isFile() && file.exists() && file.canWrite()) {
                file.delete();
            }
        }
        return zipfile;
    } catch (Exception e) {
        LOGGER.error("FAILED TO ZIP FILES", e);
    }
    return null;
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ZIP?/*from  w  w w  . j  a  va2  s .co m*/
 * 
 * @param dirPath 
 * @param file 
 * @param zouts ?
 */
public static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) {
    FileInputStream fin = null;
    ZipEntry entry = null;
    // ?
    byte[] buf = new byte[4096];
    int readByte = 0;
    if (file.isFile()) {
        try {
            // ?
            fin = new FileInputStream(file);
            // ZipEntry
            entry = new ZipEntry(getEntryName(dirPath, file));
            // ?
            zouts.putNextEntry(entry);
            // ?
            while ((readByte = fin.read(buf)) != -1) {
                zouts.write(buf, 0, readByte);
            }
            zouts.closeEntry();
            fin.close();
            System.out.println(" " + file.getAbsolutePath() + " zip!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

/**
 * @param basePath/*from  ww  w . java 2s.  c  om*/
 * @param zipPath
 * @param filePaths
 * @throws java.io.IOException
 */
public static void zip(File basePath, File zipPath, Map<String, String> filePaths) throws IOException {
    BufferedInputStream origin = null;
    ZipOutputStream out = null;
    FileOutputStream dest = null;
    try {
        dest = new FileOutputStream(zipPath);
        out = new ZipOutputStream(new BufferedOutputStream(dest));
        //out.setMethod(ZipOutputStream.DEFLATED);
        byte data[] = new byte[BUFFER];
        for (Map.Entry<String, String> file : filePaths.entrySet()) {
            String filename = file.getKey();
            String filePath = file.getValue();
            System.out.println("Adding: " + basePath.getPath() + filePath + " => " + filename);
            File f = new File(basePath, filePath);
            if (f.exists()) {
                FileInputStream fi = new FileInputStream(f);
                origin = new BufferedInputStream(fi, BUFFER);
                ZipEntry entry = new ZipEntry(filename);
                entry.setCrc(FileUtils.checksumCRC32(new File(basePath, filePath)));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }
                origin.close();
            }
        }
    } finally {
        if (origin != null)
            origin.close();
        if (out != null)
            out.close();
        if (dest != null)
            dest.close();
    }
}