Example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream

Introduction

In this page you can find the example usage for org.apache.commons.compress.archivers.zip ZipArchiveInputStream ZipArchiveInputStream.

Prototype

public ZipArchiveInputStream(InputStream inputStream) 

Source Link

Usage

From source file:cz.muni.fi.xklinec.zipstream.App.java

/**
 * Entry point. /*from ww w. j a  va  2  s  .  com*/
 * 
 * @param args
 * @throws FileNotFoundException
 * @throws IOException
 * @throws NoSuchFieldException
 * @throws ClassNotFoundException
 * @throws NoSuchMethodException 
 */
public static void main(String[] args) throws FileNotFoundException, IOException, NoSuchFieldException,
        ClassNotFoundException, NoSuchMethodException, InterruptedException {
    OutputStream fos = null;
    InputStream fis = null;

    if ((args.length != 0 && args.length != 2)) {
        System.err.println(String.format("Usage: app.jar source.apk dest.apk"));
        return;
    } else if (args.length == 2) {
        System.err.println(
                String.format("Will use file [%s] as input file and [%s] as output file", args[0], args[1]));
        fis = new FileInputStream(args[0]);
        fos = new FileOutputStream(args[1]);
    } else if (args.length == 0) {
        System.err.println(String.format("Will use file [STDIN] as input file and [STDOUT] as output file"));
        fis = System.in;
        fos = System.out;
    }

    final Deflater def = new Deflater(9, true);
    ZipArchiveInputStream zip = new ZipArchiveInputStream(fis);

    // List of postponed entries for further "processing".
    List<PostponedEntry> peList = new ArrayList<PostponedEntry>(6);

    // Output stream
    ZipArchiveOutputStream zop = new ZipArchiveOutputStream(fos);
    zop.setLevel(9);

    // Read the archive
    ZipArchiveEntry ze = zip.getNextZipEntry();
    while (ze != null) {

        ZipExtraField[] extra = ze.getExtraFields(true);
        byte[] lextra = ze.getLocalFileDataExtra();
        UnparseableExtraFieldData uextra = ze.getUnparseableExtraFieldData();
        byte[] uextrab = uextra != null ? uextra.getLocalFileDataData() : null;

        // ZipArchiveOutputStream.DEFLATED
        // 

        // Data for entry
        byte[] byteData = Utils.readAll(zip);
        byte[] deflData = new byte[0];
        int infl = byteData.length;
        int defl = 0;

        // If method is deflated, get the raw data (compress again).
        if (ze.getMethod() == ZipArchiveOutputStream.DEFLATED) {
            def.reset();
            def.setInput(byteData);
            def.finish();

            byte[] deflDataTmp = new byte[byteData.length * 2];
            defl = def.deflate(deflDataTmp);

            deflData = new byte[defl];
            System.arraycopy(deflDataTmp, 0, deflData, 0, defl);
        }

        System.err.println(String.format(
                "ZipEntry: meth=%d " + "size=%010d isDir=%5s " + "compressed=%07d extra=%d lextra=%d uextra=%d "
                        + "comment=[%s] " + "dataDesc=%s " + "UTF8=%s " + "infl=%07d defl=%07d " + "name [%s]",
                ze.getMethod(), ze.getSize(), ze.isDirectory(), ze.getCompressedSize(),
                extra != null ? extra.length : -1, lextra != null ? lextra.length : -1,
                uextrab != null ? uextrab.length : -1, ze.getComment(),
                ze.getGeneralPurposeBit().usesDataDescriptor(), ze.getGeneralPurposeBit().usesUTF8ForNames(),
                infl, defl, ze.getName()));

        final String curName = ze.getName();

        // META-INF files should be always on the end of the archive, 
        // thus add postponed files right before them
        if (curName.startsWith("META-INF") && peList.size() > 0) {
            System.err.println(
                    "Now is the time to put things back, but at first, I'll perform some \"facelifting\"...");

            // Simulate som evil being done
            Thread.sleep(5000);

            System.err.println("OK its done, let's do this.");
            for (PostponedEntry pe : peList) {
                System.err.println(
                        "Adding postponed entry at the end of the archive! deflSize=" + pe.deflData.length
                                + "; inflSize=" + pe.byteData.length + "; meth: " + pe.ze.getMethod());

                pe.dump(zop, false);
            }

            peList.clear();
        }

        // Capturing interesting files for us and store for later.
        // If the file is not interesting, send directly to the stream.
        if ("classes.dex".equalsIgnoreCase(curName) || "AndroidManifest.xml".equalsIgnoreCase(curName)) {
            System.err.println("### Interesting file, postpone sending!!!");

            PostponedEntry pe = new PostponedEntry(ze, byteData, deflData);
            peList.add(pe);
        } else {
            // Write ZIP entry to the archive
            zop.putArchiveEntry(ze);
            // Add file data to the stream
            zop.write(byteData, 0, infl);
            zop.closeArchiveEntry();
        }

        ze = zip.getNextZipEntry();
    }

    // Cleaning up stuff
    zip.close();
    fis.close();

    zop.finish();
    zop.close();
    fos.close();

    System.err.println("THE END!");
}

From source file:eml.studio.server.file.FileUploadServlet.java

public static void unZipFiles(InputStream instream, String ID) throws IOException {
    ZipArchiveInputStream zin = new ZipArchiveInputStream(instream);
    java.util.zip.ZipEntry entry = null;
    while ((entry = zin.getNextZipEntry()) != null) {
        String zipEntryName = entry.getName();
        String outPath = zipEntryName.replaceAll("\\*", "/");
        String path = "lib";
        path += zipEntryName.substring(zipEntryName.indexOf('/'), zipEntryName.length());
        System.out.println("[path ]:" + path);
        if (!outPath.endsWith("/")) {
            InputStream in = zin;
            HDFSIO.uploadModel("/" + ID + "/" + path, in);
        }/*w w  w. j  a v a2 s  .  com*/
    }
    zin.close();
}

From source file:com.googlecode.t7mp.util.ZipUtil.java

public static void unzip(InputStream warInputStream, File destination) {
    try {/*from   w  ww.  ja  v  a 2 s . c  o m*/
        ZipArchiveInputStream in = null;
        try {
            in = new ZipArchiveInputStream(warInputStream);

            ZipArchiveEntry entry = null;
            while ((entry = in.getNextZipEntry()) != null) {
                File outfile = new File(destination.getCanonicalPath() + "/" + entry.getName());
                outfile.getParentFile().mkdirs();
                if (entry.isDirectory()) {
                    outfile.mkdir();
                    continue;
                }
                OutputStream o = new FileOutputStream(outfile);
                try {
                    IOUtils.copy(in, o);
                } finally {
                    o.close();
                }
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        warInputStream.close();
    } catch (FileNotFoundException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    } catch (IOException e) {
        throw new TomcatSetupException(e.getMessage(), e);
    }
}

From source file:com.googlecode.dex2jar.reader.CCZipExtractor.java

@Override
public byte[] extract(byte[] data, String name) throws IOException {
    ZipArchiveInputStream zis = null;//from w ww . j  a  va 2  s . c o  m
    try {
        zis = new ZipArchiveInputStream(new ByteArrayInputStream(data));
        for (ZipArchiveEntry e = zis.getNextZipEntry(); e != null; e = zis.getNextZipEntry()) {
            e.getGeneralPurposeBit().useEncryption(false);
            if (e.getName().equals(name)) {
                data = IOUtils.toByteArray(zis);
                zis.close();
                return data;
            }
        }
    } finally {
        IOUtils.closeQuietly(zis);
    }
    throw new IOException("can't find classes.dex in the zip");
}

From source file:io.github.blindio.prospero.core.browserdrivers.phantomjs.ZipUnArchiver.java

public void extract() {

    /** create a TarArchiveInputStream object. **/
    try {//  ww  w  .j av a 2  s  .  c om
        FileInputStream fin = new FileInputStream(getSourceFile());
        BufferedInputStream in = new BufferedInputStream(fin);
        ArchiveInputStream arcIn = new ZipArchiveInputStream(in);

        extract(arcIn);
    } catch (IOException ioe) {
        throw new ProsperoIOException(ioe);
    }
}

From source file:io.github.runassudo.gtfs.ZipStreamGTFSFile.java

public void iterateThroughContents(IterateThroughContentsCallback callback) throws IOException {
    ZipArchiveInputStream gtfsStream = new ZipArchiveInputStream(parentFile.getInputStream(zipEntry));
    ZipArchiveEntry contentEntry;/* ww w.  jav  a2 s. com*/
    while ((contentEntry = gtfsStream.getNextZipEntry()) != null) {
        callback.call(new ZipStreamGTFSCSV(gtfsStream, contentEntry));
    }
}

From source file:fr.gael.ccsds.sip.archive.ZipArchiveManager.java

/**
 * Produces Zip compressed archive.//from w w  w.ja  v a2s.c  om
 */
@Override
public File copy(final File src, final File zip_file, final String dst) throws Exception {
    if (zip_file.exists()) {
        final FileInputStream fis = new FileInputStream(zip_file);
        final ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);

        final File tempFile = File.createTempFile("updateZip", "zip");
        final FileOutputStream fos = new FileOutputStream(tempFile);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        // copy the existing entries
        ZipArchiveEntry nextEntry;
        while ((nextEntry = zis.getNextZipEntry()) != null) {
            zos.putArchiveEntry(nextEntry);
            IOUtils.copy(zis, zos);
            zos.closeArchiveEntry();
        }

        // create the new entry
        final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst);
        entry.setSize(src.length());
        zos.putArchiveEntry(entry);
        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, zos);
        sfis.close();
        zos.closeArchiveEntry();

        zos.finish();
        zis.close();
        fis.close();
        zos.close();

        // Rename the new file over the old
        boolean status = zip_file.delete();
        File saved_tempFile = tempFile;
        status = tempFile.renameTo(zip_file);

        // Copy the new file over the old if the renaming failed
        if (!status) {
            final FileInputStream tfis = new FileInputStream(saved_tempFile);
            final FileOutputStream tfos = new FileOutputStream(zip_file);

            final byte[] buf = new byte[1024];
            int i = 0;

            while ((i = tfis.read(buf)) != -1) {
                tfos.write(buf, 0, i);
            }

            tfis.close();
            tfos.close();

            saved_tempFile.delete();
        }

        return zip_file;

    } else {
        final FileOutputStream fos = new FileOutputStream(zip_file);
        final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);

        final ZipArchiveEntry entry = new ZipArchiveEntry(src, dst);
        entry.setSize(src.length());
        zos.putArchiveEntry(entry);

        final FileInputStream sfis = new FileInputStream(src);
        IOUtils.copy(sfis, zos);
        sfis.close();

        zos.closeArchiveEntry();

        zos.finish();
        zos.close();
        fos.close();
    }
    return zip_file;
}

From source file:com.streamsets.datacollector.restapi.ZipEdgeArchiveBuilder.java

@Override
public void finish() throws IOException {
    try (ZipArchiveOutputStream zipArchiveOutput = new ZipArchiveOutputStream(outputStream);
            ZipArchiveInputStream zipArchiveInput = new ZipArchiveInputStream(
                    new FileInputStream(edgeArchive))) {
        ZipArchiveEntry entry = zipArchiveInput.getNextZipEntry();

        while (entry != null) {
            zipArchiveOutput.putArchiveEntry(entry);
            IOUtils.copy(zipArchiveInput, zipArchiveOutput);
            zipArchiveOutput.closeArchiveEntry();
            entry = zipArchiveInput.getNextZipEntry();
        }/*w ww  . j av a  2  s.  com*/

        for (PipelineConfigurationJson pipelineConfiguration : pipelineConfigurationList) {
            addArchiveEntry(zipArchiveOutput, pipelineConfiguration, pipelineConfiguration.getPipelineId(),
                    PIPELINE_JSON_FILE);
            addArchiveEntry(zipArchiveOutput, pipelineConfiguration.getInfo(),
                    pipelineConfiguration.getPipelineId(), PIPELINE_INFO_FILE);
        }

        zipArchiveOutput.finish();
    }
}

From source file:de.flapdoodle.embedmongo.extract.ZipExtractor.java

@Override
public void extract(RuntimeConfig runtime, File source, File destination, Pattern file) throws IOException {
    IProgressListener progressListener = runtime.getProgressListener();
    String progressLabel = "Extract " + source;
    progressListener.start(progressLabel);

    FileInputStream fin = new FileInputStream(source);
    BufferedInputStream in = new BufferedInputStream(fin);

    ZipArchiveInputStream zipIn = new ZipArchiveInputStream(in);
    try {//from w  ww. jav  a  2 s.  c o m
        ZipArchiveEntry entry;
        while ((entry = zipIn.getNextZipEntry()) != null) {
            if (file.matcher(entry.getName()).matches()) {
                //               System.out.println("File: " + entry.getName());
                if (zipIn.canReadEntryData(entry)) {
                    //                  System.out.println("Can Read: " + entry.getName());
                    long size = entry.getSize();
                    Files.write(zipIn, size, destination);
                    destination.setExecutable(true);
                    //                  System.out.println("DONE");
                    progressListener.done(progressLabel);
                }
                break;

            } else {
                //               System.out.println("SKIP File: " + entry.getName());
            }
        }

    } finally {
        zipIn.close();
    }

}

From source file:io.github.runassudo.gtfs.ZipStreamGTFSFile.java

public FlatGTFSFile toFlatFile() throws IOException {
    File destBase = new File(
            new File(GTFSCollection.cacheDir,
                    Hashing.sha256().hashString(parentFile.getName(), StandardCharsets.UTF_8).toString()),
            Hashing.sha256().hashString(zipEntry.getName(), StandardCharsets.UTF_8).toString());

    if (!destBase.exists()) {
        ZipArchiveInputStream gtfsStream = new ZipArchiveInputStream(parentFile.getInputStream(zipEntry));
        ZipArchiveEntry contentEntry;/* www.j  a  va2s.c o m*/
        while ((contentEntry = gtfsStream.getNextZipEntry()) != null) {
            // Copy this file to cache
            File dest = new File(destBase, contentEntry.getName());
            dest.getParentFile().mkdirs();
            FileOutputStream os = new FileOutputStream(dest);
            byte[] buf = new byte[4096];
            int len;
            while ((len = gtfsStream.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
            os.close();
        }
        gtfsStream.close();
    }

    return new FlatGTFSFile(destBase);
}