Example usage for java.util.zip ZipInputStream close

List of usage examples for java.util.zip ZipInputStream close

Introduction

In this page you can find the example usage for java.util.zip ZipInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java

public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException {
    if (outputFile.exists()) {
        FileUtils.deleteQuietly(outputFile);
    }/*from w  ww. java  2s. com*/
    if (null == includeEnties || includeEnties.size() < 1) {
        return;
    }
    FileOutputStream fos = new FileOutputStream(outputFile);
    ZipOutputStream jos = new ZipOutputStream(fos);
    final byte[] buffer = new byte[8192];
    FileInputStream fis = new FileInputStream(inputFile);
    ZipInputStream zis = new ZipInputStream(fis);

    try {
        // loop on the entries of the jar file package and put them in the final jar
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory()) {
                continue;
            }
            String name = entry.getName();
            if (!includeEnties.contains(name)) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            // add the entry to the jar archive
            jos.putNextEntry(newEntry);

            // read the content of the entry from the input stream, and write it into the archive.
            int count;
            while ((count = zis.read(buffer)) != -1) {
                jos.write(buffer, 0, count);
            }
            // close the entries for this file
            jos.closeEntry();
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
    fis.close();
    jos.close();
}

From source file:com.haha01haha01.harail.DatabaseDownloader.java

private boolean unpackZip(File output_dir, File zipname) {
    InputStream is;/*from w w w.jav a  2  s .c o  m*/
    ZipInputStream zis;
    try {
        String filename;
        is = new FileInputStream(zipname);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            filename = ze.getName();

            // Need to create directories if not exists, or
            // it will generate an Exception...
            if (ze.isDirectory()) {
                File fmd = new File(output_dir, filename);
                fmd.mkdirs();
                continue;
            }

            FileOutputStream fout = new FileOutputStream(new File(output_dir, filename));

            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }

            fout.close();
            zis.closeEntry();
        }

        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.mindquarry.desktop.workspace.SVNTestBase.java

/**
 * Extracts the zip file <code>zipName</code> into the folder
 * <code>destinationPath</code>.
 *//*from   w  w  w.  ja v a2  s  .  c om*/
private void extractZip(String zipName, String destinationPath) throws IOException {
    File dest = new File(destinationPath);
    // delete if test has failed and extracted dir is still present
    FileUtils.deleteDirectory(dest);
    dest.mkdirs();

    byte[] buffer = new byte[1024];
    ZipEntry zipEntry;
    ZipInputStream zipInputStream = new ZipInputStream(
            new FileInputStream("src/test/resources/com/mindquarry/desktop/workspace/" + zipName));

    while (null != (zipEntry = zipInputStream.getNextEntry())) {

        File zippedFile = new File(destinationPath + zipEntry.getName());

        if (zipEntry.isDirectory()) {
            zippedFile.mkdirs();
        } else {
            // ensure the parent directory exists
            zippedFile.getParentFile().mkdirs();

            OutputStream fileOutStream = new FileOutputStream(zippedFile);
            transferBytes(zipInputStream, fileOutStream, buffer);
            fileOutStream.close();
        }
    }

    zipInputStream.close();
}

From source file:com.adobe.communities.ugc.migration.importer.MessagesImportServlet.java

protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response)
        throws ServletException, IOException {

    final ResourceResolver resolver = request.getResourceResolver();

    UGCImportHelper.checkUserPrivileges(resolver, rrf);

    // first check to see if the caller provided an explicit selector for our MessagingOperationsService
    final RequestParameter serviceSelector = request.getRequestParameter("serviceSelector");
    // if no serviceSelector parameter was provided, we'll try going with whatever we get through the Reference
    if (null != serviceSelector && !serviceSelector.getString().equals("")) {
        // make sure the messagingServiceTracker was created by the activate method
        if (null == messagingServiceTracker) {
            throw new ServletException("Cannot use messagingServiceTracker");
        }/*from   www  .j  av a2  s  .co m*/
        // search for the MessagingOperationsService corresponding to the provided selector
        messagingService = messagingServiceTracker.getService(serviceSelector.getString());
        if (null == messagingService) {
            throw new ServletException(
                    "MessagingOperationsService for selector " + serviceSelector.getString() + "was not found");
        }
    }
    // finally get the uploaded file
    final RequestParameter[] fileRequestParameters = request.getRequestParameters("file");
    if (fileRequestParameters != null && fileRequestParameters.length > 0
            && !fileRequestParameters[0].isFormField()) {

        final Map<String, Object> messageModifiers = new HashMap<String, Object>();
        if (fileRequestParameters[0].getFileName().endsWith(".json")) {
            // if upload is a single json file...
            final InputStream inputStream = fileRequestParameters[0].getInputStream();
            final JsonParser jsonParser = new JsonFactory().createParser(inputStream);
            jsonParser.nextToken(); // get the first token
            importMessages(request, jsonParser, messageModifiers);
        } else if (fileRequestParameters[0].getFileName().endsWith(".zip")) {
            ZipInputStream zipInputStream;
            try {
                zipInputStream = new ZipInputStream(fileRequestParameters[0].getInputStream());
            } catch (IOException e) {
                throw new ServletException("Could not open zip archive");
            }
            ZipEntry zipEntry = zipInputStream.getNextEntry();
            while (zipEntry != null) {
                final JsonParser jsonParser = new JsonFactory().createParser(zipInputStream);
                jsonParser.nextToken(); // get the first token
                importMessages(request, jsonParser, messageModifiers);
                zipInputStream.closeEntry();
                zipEntry = zipInputStream.getNextEntry();
            }
            zipInputStream.close();
        } else {
            throw new ServletException("Unrecognized file input type");
        }
        if (!messageModifiers.isEmpty()) {
            try {
                Thread.sleep(3000); //wait 3 seconds to allow the messages to be indexed by solr for search
            } catch (final InterruptedException e) {
                // do nothing.
            }
            updateMessageDetails(request, messageModifiers);
        }
    } else {
        throw new ServletException("No file provided for UGC data");
    }
}

From source file:com.espringtran.compressor4j.processor.LzmaProcessor.java

/**
 * Read from compressed file/*from   w w  w .j  a  v  a2s  . c om*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    LZMACompressorInputStream cis = new LZMACompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}

From source file:com.taobao.android.utils.ZipUtils.java

/**
 * zip/*  w ww .  ja v  a2s .  c o m*/
 *
 * @param zipFile
 * @param file
 * @param destPath  ?
 * @param overwrite ?
 * @throws 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 (ZipEntry.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:com.zimbra.qa.unittest.TestUserServlet.java

private void verifyZipFile(ZMailbox mbox, String relativePath, boolean hasBody) throws Exception {
    InputStream in = mbox.getRESTResource(relativePath);
    ZipInputStream zipIn = new ZipInputStream(in);
    ZipEntry entry;/* w  ww.  j  a  v a  2  s  .c  o  m*/
    boolean foundMessage = false;
    while ((entry = zipIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".eml")) {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            ByteUtil.copy(zipIn, false, buf, true);
            byte[] content = buf.toByteArray();
            MimeMessage message = new ZMimeMessage(JMSession.getSession(),
                    new SharedByteArrayInputStream(content));
            byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
            if (hasBody) {
                assertTrue(entry.getName() + " has no body", body.length > 0);
            } else {
                assertEquals(entry.getName() + " has a body", 0, body.length);
            }
            foundMessage = true;
        }
    }
    zipIn.close();
    assertTrue(foundMessage);
}

From source file:org.eclipse.birt.build.mavenrepogen.RepoGen.java

private Manifest getManifest(final File file) throws IOException {
    final ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
    try {/*from   ww w.  j  a  va2  s  .  c o m*/
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            // read the manifest to determine the name and version number
            // System.out.println(entry.getName() + " " + entry.isDirectory());
            if ("META-INF/MANIFEST.MF".equals(entry.getName()))
                return new Manifest(zis);
            entry = zis.getNextEntry();
        }
    } finally {
        zis.close();
    }
    return null;
}

From source file:com.theaetetuslabs.apkmakertester.ApkMakerService.java

private boolean unpackZip(String pathToZip, String destPath) {
    new File(destPath).mkdirs();
    InputStream is;//from  w  w  w . j a va  2 s  . c  om
    ZipInputStream zis;
    try {
        String filename;
        is = new FileInputStream(pathToZip);
        zis = new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        byte[] buffer = new byte[1024];
        int count;

        while ((ze = zis.getNextEntry()) != null) {
            // zapis do souboru
            filename = ze.getName();

            // Need to create directories if not exists, or
            // it will generate an Exception...
            if (ze.isDirectory()) {
                File fmd = new File(destPath, filename);
                fmd.mkdirs();
                continue;
            }

            FileOutputStream fout = new FileOutputStream(new File(destPath, filename));

            // cteni zipu a zapis
            while ((count = zis.read(buffer)) != -1) {
                fout.write(buffer, 0, count);
            }

            fout.close();
            zis.closeEntry();
        }

        zis.close();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:com.espringtran.compressor4j.processor.Bzip2Processor.java

/**
 * Read from compressed file/*from   ww w .  j  ava 2s .  c  o m*/
 * 
 * @param srcPath
 *            path of compressed file
 * @param fileCompressor
 *            FileCompressor object
 * @throws Exception
 */
@Override
public void read(String srcPath, FileCompressor fileCompressor) throws Exception {
    long t1 = System.currentTimeMillis();
    byte[] data = FileUtil.convertFileToByte(srcPath);
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    BZip2CompressorInputStream cis = new BZip2CompressorInputStream(bais);
    ZipInputStream zis = new ZipInputStream(cis);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[1024];
        int readByte;
        ZipEntry entry = zis.getNextEntry();
        while (entry != null) {
            long t2 = System.currentTimeMillis();
            baos = new ByteArrayOutputStream();
            readByte = zis.read(buffer);
            while (readByte != -1) {
                baos.write(buffer, 0, readByte);
                readByte = zis.read(buffer);
            }
            zis.closeEntry();
            BinaryFile binaryFile = new BinaryFile(entry.getName(), baos.toByteArray());
            fileCompressor.addBinaryFile(binaryFile);
            LogUtil.createAddFileLog(fileCompressor, binaryFile, t2, System.currentTimeMillis());
            entry = zis.getNextEntry();
        }
    } catch (Exception e) {
        FileCompressor.LOGGER.error("Error on get compressor file", e);
    } finally {
        baos.close();
        zis.close();
        cis.close();
        bais.close();
    }
    LogUtil.createReadLog(fileCompressor, srcPath, data.length, t1, System.currentTimeMillis());
}