Example usage for java.util.zip ZipInputStream ZipInputStream

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

Introduction

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

Prototype

public ZipInputStream(InputStream in) 

Source Link

Document

Creates a new ZIP input stream.

Usage

From source file:com.heliosdecompiler.helios.LoadedFile.java

private void readDataQuick() {
    // This code should run as fast as possible
    byte[] data = null;
    try (FileInputStream inputStream = new FileInputStream(file)) {
        data = IOUtils.toByteArray(inputStream);
    } catch (IOException ex) {
        ExceptionHandler.handle(new RuntimeException("Error while reading file", ex));
        return;//from www  .  j a v a 2  s  . com
    }
    // And now we can take our time. The file has been unlocked (unless something went seriously wrong)
    this.files = new HashMap<>();
    try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(data))) {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            this.files.put(entry.getName(), IOUtils.toByteArray(zipInputStream));
        }
    } catch (Throwable ex) {
        // This should never happen
        ExceptionHandler.handle(new RuntimeException("Error while parsing file (!)", ex));
        return;
    }
    // If files is still empty, then it's not a zip file (or something weird happened)
    if (this.files.size() == 0) {
        this.files.put(file.getName(), data);
    }
    // Lock the map
    this.files = Collections.unmodifiableMap(this.files);
}

From source file:com.yifanlu.PSXperiaTool.Extractor.CrashBandicootExtractor.java

private void extractZip(File zipFile, File output, FileFilter filter) throws IOException {
    Logger.info("Extracting ZIP file: %s to: %s", zipFile.getPath(), output.getPath());
    if (!output.exists())
        output.mkdirs();/*from  w  w w. j  ava  2 s.  c o m*/
    ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry entry;
    while ((entry = zip.getNextEntry()) != null) {
        File file = new File(output, entry.getName());
        if (file.isDirectory())
            continue;
        if (filter != null && !filter.accept(file))
            continue;
        Logger.verbose("Unzipping %s", entry.getName());
        FileUtils.touch(file);
        FileOutputStream out = new FileOutputStream(file.getPath());
        int n;
        byte[] buffer = new byte[BLOCK_SIZE];
        while ((n = zip.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        out.close();
        zip.closeEntry();
        Logger.verbose("Done extracting %s", entry.getName());
    }
    zip.close();
    Logger.debug("Done extracting ZIP.");
}

From source file:gov.nih.nci.caarray.web.action.project.ProjectSourcesActionTest.java

@Test
public void testDownload() throws Exception {
    assertEquals("noSourceData", this.action.download());

    final CaArrayFile rawFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_IDF);
    final CaArrayFile derivedFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_SDRF);

    final Project p = new Project();
    p.getExperiment().setPublicIdentifier("test");
    final Source so = new Source();
    final Sample s1 = new Sample();
    so.getSamples().add(s1);/*from  ww w . ja v a2  s .  c  o  m*/
    final Sample s2 = new Sample();
    so.getSamples().add(s2);
    final Extract e = new Extract();
    s1.getExtracts().add(e);
    s2.getExtracts().add(e);
    final LabeledExtract le = new LabeledExtract();
    e.getLabeledExtracts().add(le);
    final Hybridization h = new Hybridization();
    le.getHybridizations().add(h);
    final RawArrayData raw = new RawArrayData();
    h.addArrayData(raw);
    final DerivedArrayData derived = new DerivedArrayData();
    h.getDerivedDataCollection().add(derived);
    raw.setDataFile(rawFile);
    derived.setDataFile(derivedFile);

    this.action.setCurrentSource(so);
    this.action.setProject(p);
    final List<CaArrayFile> files = new ArrayList<CaArrayFile>(so.getAllDataFiles());
    Collections.sort(files, DownloadHelper.CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
    assertEquals(2, files.size());
    assertEquals("missing_term_source.idf", files.get(0).getName());
    assertEquals("missing_term_source.sdrf", files.get(1).getName());

    this.action.download();
    assertEquals("application/zip", this.mockResponse.getContentType());
    assertEquals("filename=\"caArray_test_files.zip\"", this.mockResponse.getHeader("Content-disposition"));

    final ZipInputStream zis = new ZipInputStream(
            new ByteArrayInputStream(this.mockResponse.getContentAsByteArray()));
    ZipEntry ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.idf", ze.getName());
    ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.sdrf", ze.getName());
    assertNull(zis.getNextEntry());
    IOUtils.closeQuietly(zis);
}

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

private boolean unpackZip(File output_dir, File zipname) {
    InputStream is;//w  w w .  jav  a2s. co  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:ingest.utility.IngestUtilities.java

/**
 * Unzip the given zip into output directory. This is only applicable for SHAPEFILES as it includes black/whitelist
 * for preventing malicious inputs./*w  w w  . jav  a2s. c o m*/
 * 
 * @param zipPath
 *            Zip file full path
 * @param extractPath
 *            Extracted zip output directory
 * 
 * @return boolean if successful
 * @throws IOException
 */
public void extractZip(String zipPath, String extractPath) throws IOException {
    try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipPath))) {
        // Create output directory
        File directory = new File(extractPath);
        if (!directory.exists()) {
            directory.mkdir();
        }

        // Get initial file list entry
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            extractZipEntry(zipEntry.getName(), zipInputStream, zipPath, extractPath);

            zipEntry = zipInputStream.getNextEntry();
        }

        zipInputStream.closeEntry();
    } catch (IOException ex) {
        String error = "Unable to extract zip: " + zipPath + " to path " + extractPath;
        LOG.error(error, ex, new AuditElement(INGEST, "failedExtractShapefileZip", extractPath));
        throw new IOException(error);
    }
}

From source file:gov.nih.nci.caarray.web.action.project.ProjectLabeledExtractsActionTest.java

@Test
public void testDownload() throws Exception {
    assertEquals("noLabeledExtractData", this.action.download());

    final CaArrayFile rawFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_IDF);
    final CaArrayFile derivedFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_SDRF);

    final Project p = new Project();
    p.getExperiment().setPublicIdentifier("test");
    final LabeledExtract le = new LabeledExtract();
    final Hybridization h1 = new Hybridization();
    le.getHybridizations().add(h1);/*from   w  w  w  .  jav  a  2  s  . c om*/
    final Hybridization h2 = new Hybridization();
    le.getHybridizations().add(h2);
    final RawArrayData raw = new RawArrayData();
    h1.addArrayData(raw);
    final DerivedArrayData derived = new DerivedArrayData();
    h2.getDerivedDataCollection().add(derived);
    raw.setDataFile(rawFile);
    derived.setDataFile(derivedFile);

    this.action.setCurrentLabeledExtract(le);
    this.action.setProject(p);
    final List<CaArrayFile> files = new ArrayList<CaArrayFile>(le.getAllDataFiles());
    Collections.sort(files, DownloadHelper.CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
    assertEquals(2, files.size());
    assertEquals("missing_term_source.idf", files.get(0).getName());
    assertEquals("missing_term_source.sdrf", files.get(1).getName());

    this.action.download();
    assertEquals("application/zip", this.mockResponse.getContentType());
    assertEquals("filename=\"caArray_test_files.zip\"", this.mockResponse.getHeader("Content-disposition"));

    final ZipInputStream zis = new ZipInputStream(
            new ByteArrayInputStream(this.mockResponse.getContentAsByteArray()));
    ZipEntry ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.idf", ze.getName());
    ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.sdrf", ze.getName());
    assertNull(zis.getNextEntry());
    IOUtils.closeQuietly(zis);
}

From source file:ch.puzzle.itc.mobiliar.business.shakedown.control.ShakedownTestRunner.java

private String analyzeResult(STS sts) throws ShakedownTestException {
    String testResultPath = ConfigurationService.getProperty(ConfigKey.TEST_RESULT_PATH);
    try {/*  w w  w  .  j av  a 2s  .  c o m*/
        StringBuilder sb = new StringBuilder();
        ZipInputStream zis = null;
        try {
            zis = new ZipInputStream(
                    new FileInputStream(testResultPath + File.separator + sts.getTestId() + ".zip"));
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (entry.getName().equals("/result.xml")) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(zis));
                    String s;
                    while ((s = reader.readLine()) != null) {
                        sb.append(s);
                    }
                }
            }
        } finally {
            if (zis != null) {
                zis.close();
            }
        }
        return sb.toString();
    } catch (FileNotFoundException e) {
        throw new ShakedownTestException("Was not able to analyze the result - no result file found!", e);
    } catch (IOException e) {
        throw new ShakedownTestException("Was not able to analyze the result", e);
    }

}

From source file:gov.nih.nci.caarray.web.action.project.ProjectExtractsActionTest.java

@Test
public void testDownload() throws Exception {
    final CaArrayFile rawFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_IDF);
    final CaArrayFile derivedFile = this.fasStub.add(MageTabDataFiles.MISSING_TERMSOURCE_SDRF);

    final Project p = new Project();
    p.getExperiment().setPublicIdentifier("test");
    final Extract e1 = new Extract();
    final Extract e2 = new Extract();
    final LabeledExtract le1 = new LabeledExtract();
    e1.getLabeledExtracts().add(le1);/*from ww w .  j av  a 2  s  . com*/
    final LabeledExtract le2 = new LabeledExtract();
    e2.getLabeledExtracts().add(le2);
    final Hybridization h = new Hybridization();
    le2.getHybridizations().add(h);
    final RawArrayData raw = new RawArrayData();
    h.addArrayData(raw);
    final DerivedArrayData derived = new DerivedArrayData();
    h.getDerivedDataCollection().add(derived);
    raw.setDataFile(rawFile);
    derived.setDataFile(derivedFile);

    this.action.setCurrentExtract(e1);
    this.action.setProject(p);
    assertEquals("noExtractData", this.action.download());
    this.action.setCurrentExtract(e2);

    final List<CaArrayFile> files = new ArrayList<CaArrayFile>(e2.getAllDataFiles());
    Collections.sort(files, DownloadHelper.CAARRAYFILE_NAME_COMPARATOR_INSTANCE);
    assertEquals(2, files.size());
    assertEquals("missing_term_source.idf", files.get(0).getName());
    assertEquals("missing_term_source.sdrf", files.get(1).getName());

    this.action.download();
    assertEquals("application/zip", this.mockResponse.getContentType());
    assertEquals("filename=\"caArray_test_files.zip\"", this.mockResponse.getHeader("Content-disposition"));

    final ZipInputStream zis = new ZipInputStream(
            new ByteArrayInputStream(this.mockResponse.getContentAsByteArray()));
    ZipEntry ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.idf", ze.getName());
    ze = zis.getNextEntry();
    assertNotNull(ze);
    assertEquals("missing_term_source.sdrf", ze.getName());
    assertNull(zis.getNextEntry());
    IOUtils.closeQuietly(zis);
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static List<String> getDirsList(OmniFile zipOmni) {

    ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream());

    List<String> arrayList = new ArrayList<>();

    ZipEntry entry = null;//from ww w .ja v a 2s. c o  m
    try {
        while ((entry = zis.getNextEntry()) != null) {

            if (entry.isDirectory())
                arrayList.add(entry.getName());
        }
        zis.close();

    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
    }

    return arrayList;
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

/*** Added by julienda - 08/09/2012
 * Return the project name by reading the first directory into the archive (.car)
 * @param path: the archive path/*from  w w w. j  a va 2 s.co m*/
 * @return filename: the project name
 * @throws IOException */
public static String getProjectName(String path) throws IOException {
    Engine.logEngine.trace("PATH: " + path);

    ZipInputStream zis = new ZipInputStream(new FileInputStream(path));
    ZipEntry ze = null;
    String fileName = null;
    try {
        if ((ze = zis.getNextEntry()) != null) {
            fileName = ze.getName().replaceAll("/.*", "");
            Engine.logEngine.trace("ZipUtils.getProjectName() - fileName: " + fileName);
        }
    } finally {
        zis.close();
    }
    return fileName;
}