Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:gov.va.chir.tagline.dao.FileDao.java

private static void saveZipFile(final File zipFile, final File... inputFiles) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];

    final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));

    for (File file : inputFiles) {
        final ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);/* ww w . j  av  a 2s  .c o m*/

        final FileInputStream in = new FileInputStream(file);
        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }

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

    zos.close();
}

From source file:com.izforge.izpack.util.IoHelper.java

public static void copyStreamToJar(InputStream zin, java.util.zip.ZipOutputStream out, String currentName,
        long fileTime) throws IOException {
    // Create new entry for zip file.
    ZipEntry newEntry = new ZipEntry(currentName);
    // Make sure there is date and time set.
    if (fileTime != -1) {
        newEntry.setTime(fileTime); // If found set it into output file.
    }//from w w  w.  j av a2s . c  om
    out.putNextEntry(newEntry);
    if (zin != null) {
        IOUtils.copy(zin, out);
    }
    out.closeEntry();
}

From source file:herddb.upgrade.ZIPUtils.java

private static void addFileToZip(int skipprefix, File file, ZipOutputStream zipper) throws IOException {
    String raw = file.getAbsolutePath().replace("\\", "/");
    if (raw.length() == skipprefix) {
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                for (File child : listFiles) {
                    addFileToZip(skipprefix, child, zipper);
                }/*from w ww.  ja  v  a  2s  . c  o m*/
            }
        }
    } else {
        String path = raw.substring(skipprefix + 1);
        if (file.isDirectory()) {
            ZipEntry entry = new ZipEntry(path);
            zipper.putNextEntry(entry);
            zipper.closeEntry();
            File[] listFiles = file.listFiles();
            if (listFiles != null) {
                for (File child : listFiles) {
                    addFileToZip(skipprefix, child, zipper);
                }
            }
        } else {
            ZipEntry entry = new ZipEntry(path);
            zipper.putNextEntry(entry);
            try (FileInputStream in = new FileInputStream(file)) {
                IOUtils.copyLarge(in, zipper);
            }
            zipper.closeEntry();
        }
    }

}

From source file:com.dreikraft.axbo.sound.SoundPackageUtil.java

private static void writeZipEntry(final String entryName, final ZipOutputStream out, final InputStream in)
        throws IOException {
    try {/*from w ww .j a  v  a2 s.  c  o m*/
        ZipEntry fileEntry = new ZipEntry(entryName);
        out.putNextEntry(fileEntry);
        final byte[] buf = new byte[BUF_SIZE];
        int avail;
        while ((avail = in.read(buf)) != -1) {
            out.write(buf, 0, avail);
        }
    } finally {
        out.flush();
        out.closeEntry();
    }
}

From source file:org.apache.oozie.tools.OozieDBExportCLI.java

private static int exportTableToJSON(Query query, ZipOutputStream zipOutputStream, String filename)
        throws IOException {
    Gson gson = new Gson();
    ZipEntry zipEntry = new ZipEntry(filename);
    zipOutputStream.putNextEntry(zipEntry);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zipOutputStream, "UTF-8"));
    query.setMaxResults(LIMIT);/*from  w w w .java 2s  .co  m*/
    int exported = 0;
    List<?> list = query.getResultList();
    while (!list.isEmpty()) {
        query.setFirstResult(exported);
        list = query.getResultList();
        for (Object w : list) {
            exported++;
            gson.toJson(w, writer);
            writer.newLine();
        }
    }
    writer.flush();
    zipOutputStream.closeEntry();
    return exported;
}

From source file:org.apache.hadoop.hive.ql.metadata.JarUtils.java

public static void jarDir(File dir, String relativePath, ZipOutputStream zos) throws IOException {
    Preconditions.checkNotNull(relativePath, "relativePath");
    Preconditions.checkNotNull(zos, "zos");

    // by JAR spec, if there is a manifest, it must be the first entry in
    // the/*from w ww  . j  a  v a2  s. c  om*/
    // ZIP.
    File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
    ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
    if (!manifestFile.exists()) {
        zos.putNextEntry(manifestEntry);
        new Manifest().write(new BufferedOutputStream(zos));
        zos.closeEntry();
    } else {
        InputStream is = new FileInputStream(manifestFile);
        copyToZipStream(is, manifestEntry, zos);
    }
    zos.closeEntry();
    zipDir(dir, relativePath, zos, true);
    zos.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   w ww.ja  v a2  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:b2s.idea.mavenize.JarCombiner.java

private static void copyContentsOf(File input, ZipOutputStream output, JarContext context) throws IOException {
    ZipFile zip = null;//from   ww w. j a v  a  2s. c  om
    try {
        zip = new ZipFile(input);
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (fileCanBeIgnored(entry, context)) {
                continue;
            }

            if (isServiceEntry(entry)) {
                context.addService(entry.getName(), contentsOf(entry, zip));
                continue;
            }

            output.putNextEntry(new ZipEntry(entry.getName()));
            output.write(contentsOf(entry, zip));
            output.flush();
            output.closeEntry();

            context.addVisitedEntry(entry);
        }
    } finally {
        close(zip);
    }
}

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   ww  w  . 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.jaspersoft.studio.community.utils.CommunityAPIUtils.java

/**
 * Creates a ZIP file using the specified zip entries.
 * // w  w  w.jav a2  s . c  o m
 * @param zipEntries the list of entries that will end up in the final zip file
 * @return the zip file reference
 * @throws CommunityAPIException
 */
public static File createZipFile(List<ZipEntry> zipEntries) throws CommunityAPIException {
    String tmpDirectory = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$
    String zipFileLocation = tmpDirectory;
    if (!(tmpDirectory.endsWith("/") || tmpDirectory.endsWith("\\"))) { //$NON-NLS-1$ //$NON-NLS-2$
        zipFileLocation += System.getProperty("file.separator"); //$NON-NLS-1$
    }
    zipFileLocation += "issueDetails.zip"; //$NON-NLS-1$

    try {
        // create byte buffer
        byte[] buffer = new byte[1024];
        // create object of FileOutputStream
        FileOutputStream fout = new FileOutputStream(zipFileLocation);
        // create object of ZipOutputStream from FileOutputStream
        ZipOutputStream zout = new ZipOutputStream(fout);

        for (ZipEntry ze : zipEntries) {
            //create object of FileInputStream for source file
            FileInputStream fin = new FileInputStream(ze.getLocation());
            zout.putNextEntry(new java.util.zip.ZipEntry(ze.getLocation()));
            // After creating entry in the zip file, actually write the file.
            int length;
            while ((length = fin.read(buffer)) > 0) {
                zout.write(buffer, 0, length);
            }
            //close the zip entry and related InputStream
            zout.closeEntry();
            fin.close();
        }
        //close the ZipOutputStream
        zout.close();
    } catch (IOException e) {
        throw new CommunityAPIException(Messages.CommunityAPIUtils_ZipCreationError, e);
    }
    return new File(zipFileLocation);
}