Example usage for java.util.zip ZipOutputStream putNextEntry

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

Introduction

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

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:net.sf.jabref.exporter.OpenDocumentSpreadsheetCreator.java

private static void addResourceFile(String name, String resource, ZipOutputStream out) throws IOException {
    ZipEntry zipEntry = new ZipEntry(name);
    out.putNextEntry(zipEntry);
    OpenDocumentSpreadsheetCreator.addFromResource(resource, out);
    out.closeEntry();/*  ww w  .j  a  va2  s. c  om*/
}

From source file:Main.java

public static void compressFiles(File files[], File fileCompressed) throws IOException {

    byte[] buffer = new byte[SIZE_OF_BUFFER];
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(fileCompressed));
    for (int i = 0; i < files.length; i++) {
        FileInputStream fileInputStream = new FileInputStream(files[i]);
        zipOutputStream.putNextEntry(new ZipEntry(files[i].getPath()));

        int size;
        while ((size = fileInputStream.read(buffer)) > 0)
            zipOutputStream.write(buffer, 0, size);

        zipOutputStream.closeEntry();//from   w  ww .j  av a2s. co  m
        fileInputStream.close();
    }

    zipOutputStream.close();
}

From source file:Main.java

public static void compressAFileToZip(File zip, File source) throws IOException {
    Log.d("source: " + source.getAbsolutePath(), ", length: " + source.length());
    Log.d("zip:", zip.getAbsolutePath());
    zip.getParentFile().mkdirs();//from   w  w w .  ja  va 2 s .c  om
    File tempFile = new File(zip.getAbsolutePath() + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ZipEntry zipEntry = new ZipEntry(source.getName());
    zipEntry.setTime(source.lastModified());
    zos.putNextEntry(zipEntry);

    FileInputStream fis = new FileInputStream(source);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] bArr = new byte[4096];
    int byteRead = 0;
    while ((byteRead = bis.read(bArr)) != -1) {
        zos.write(bArr, 0, byteRead);
    }
    zos.flush();
    zos.closeEntry();
    zos.close();
    Log.d("zipEntry: " + zipEntry, "compressedSize: " + zipEntry.getCompressedSize());
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    bis.close();
    fis.close();
    zip.delete();
    tempFile.renameTo(zip);
}

From source file:ch.rgw.compress.CompEx.java

public static byte[] CompressZIP(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[8192];
    baos.write(buf, 0, 4); // Lnge des Originalstroms
    ZipOutputStream zo = new ZipOutputStream(baos);
    zo.putNextEntry(new ZipEntry("Data"));
    int l;//  w w  w . j a v a 2 s  . c  o m
    long total = 0;
    ;
    while ((l = in.read(buf, 0, buf.length)) != -1) {
        zo.write(buf, 0, l);
        total += l;
    }
    zo.close();
    byte[] ret = baos.toByteArray();
    // Die hchstwertigen 3 Bit als Typmarker setzen
    total &= 0x1fffffff;
    total |= ZIP;
    BinConverter.intToByteArray((int) total, ret, 0);
    return ret;
}

From source file:it.geosolutions.mariss.wps.ppio.OutputResourcesPPIO.java

private static void addToZip(File f, ZipOutputStream zos) {

    FileInputStream in = null;/*ww  w.  ja  v a2  s.  c om*/
    try {
        in = new FileInputStream(f);
        zos.putNextEntry(new ZipEntry(f.getName()));
        IOUtils.copy(in, zos);
        zos.closeEntry();
    } catch (IOException e) {
        LOGGER.severe(e.getMessage());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
}

From source file:com.ibm.watson.developer_cloud.retrieve_and_rank.v1.util.ZipUtils.java

private static void writeZipEntry(ZipOutputStream out, String name, byte[] data) throws IOException {
    final ZipEntry entry = new ZipEntry(StringUtils.removeStart(name, "/"));
    out.putNextEntry(entry);
    out.write(data, 0, data.length);//from  w w w .  j  a  v  a 2  s  .c o  m
    out.closeEntry();
}

From source file:Main.java

private static boolean addEntryInputStream(ZipOutputStream zos, String entryName, InputStream inputStream) {
    final ZipEntry zipEntry = new ZipEntry(entryName);
    try {//from w w  w .ja  v a 2s  .co 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:Zip.java

/**
 * Create a OutputStream on a given file, transparently compressing the data
 * to a Zip file whose name is the provided file path, with a ".zip"
 * extension added./*from  ww  w .ja  v a  2  s. c o  m*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a OutputStream on the zip entry
 */
public static OutputStream createOutputStream(File file) {
    try {
        String path = file.getCanonicalPath();
        FileOutputStream fos = new FileOutputStream(path + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        return zos;
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (Exception ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:Zip.java

/**
 * Create a Writer on a given file, transparently compressing the data to a
 * Zip file whose name is the provided file path, with a ".zip" extension
 * added./*w  w  w. j  a v a 2  s  .  c  om*/
 *
 * @param file the file (with no zip extension)
 *
 * @return a writer on the zip entry
 */
public static Writer createWriter(File file) {
    try {
        String path = file.getCanonicalPath();
        FileOutputStream fos = new FileOutputStream(path + ".zip");
        ZipOutputStream zos = new ZipOutputStream(fos);
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        return new OutputStreamWriter(zos);
    } catch (FileNotFoundException ex) {
        System.err.println(ex.toString());
        System.err.println(file + " not found");
    } catch (Exception ex) {
        System.err.println(ex.toString());
    }

    return null;
}

From source file:ai.serotonin.backup.Backup.java

private static void addZipEntry(final ZipOutputStream zip, final String prefix, final String path)
        throws IOException {
    final ZipEntry e = new ZipEntry(path);
    zip.putNextEntry(e);

    try (FileInputStream in = new FileInputStream(new File(prefix, path))) {
        final long len = IOUtils.copy(in, zip);
        if (LOG.isDebugEnabled())
            LOG.debug("Wrote " + path + ", " + len + " bytes ");
    }//from  w w  w .  java2s  .com

    zip.closeEntry();
}