Example usage for java.util.zip ZipInputStream getNextEntry

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

Introduction

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

Prototype

public ZipEntry getNextEntry() throws IOException 

Source Link

Document

Reads the next ZIP file entry and positions the stream at the beginning of the entry data.

Usage

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

/**
 * Read from compressed file//  w  ww  . j  a v a  2  s  .com
 * 
 * @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);
    GzipCompressorInputStream cis = new GzipCompressorInputStream(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.moss.fskit.Unzipper.java

public void unzipFile(File zipFile, File destination, boolean unwrap) throws UnzipException {
    try {/*from ww w.  j  a v a  2 s  .c om*/
        ZipInputStream in = new ZipInputStream(new FileInputStream(zipFile));
        boolean done = false;
        while (!done) {
            ZipEntry nextEntry = in.getNextEntry();

            if (nextEntry == null)
                done = true;
            else {
                String name = nextEntry.getName();
                if (unwrap) {
                    name = name.substring(name.indexOf('/'));
                }
                File outputFile = new File(destination, name);
                log.info("   " + outputFile.getAbsolutePath());

                if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs())
                    throw new UnzipException("Could not create directory " + outputFile.getParent());

                if (nextEntry.isDirectory()) {
                    if (!outputFile.exists() && !outputFile.mkdir())
                        throw new UnzipException("Ddirectory does not exist and could not be created:"
                                + outputFile.getAbsolutePath());
                } else {
                    if (!outputFile.createNewFile())
                        throw new UnzipException("Could not create file " + outputFile.getAbsolutePath());
                    FileOutputStream out = new FileOutputStream(outputFile);
                    copyStreams(in, out, 1024 * 100);
                    out.close();
                    in.closeEntry();
                }

            }
        }

    } catch (Exception e) {
        throw new UnzipException("There was an error executing the unzip of file " + zipFile.getAbsolutePath(),
                e);
    }

    //      String pathOfZipFile = zipFile.getAbsolutePath();
    //      
    //      String command = "unzip -o "  + pathOfZipFile + " -d " + destination.getAbsolutePath() ;
    //      
    //      try {
    //         log.info("Running " + command);
    //         
    //         int result = new CommandRunner().runCommand(command);
    //         if(result!=0) throw new UnzipException("Unzip command exited with status " + result);
    //         log.info("Unzip complete");
    //      } catch (CommandException e) {
    //         throw new UnzipException("There was an error executing the unzip command: \"" + command + "\"", e);
    //      }
}

From source file:com.sastix.cms.server.services.content.impl.ZipFileHandlerServiceImpl.java

@Override
public DataMaps unzip(byte[] bytes) throws IOException {
    Map<String, String> foldersMap = new HashMap<>();

    Map<String, byte[]> extractedBytesMap = new HashMap<>();
    InputStream byteInputStream = new ByteArrayInputStream(bytes);
    //validate that it is a zip file
    if (isZipFile(bytes)) {
        try {//  w  w  w  .  jav a2  s .c o m
            //get the zip file content
            ZipInputStream zis = new ZipInputStream(byteInputStream);
            //get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                String fileName = ze.getName();
                if (!ze.isDirectory()) {//if entry is a directory, we should not add it as a file
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    try {
                        ByteBuffer bufIn = ByteBuffer.allocate(1024);
                        int bytesRead;
                        while ((bytesRead = zis.read(bufIn.array())) > 0) {
                            baos.write(bufIn.array(), 0, bytesRead);
                            bufIn.rewind();
                        }
                        bufIn.clear();
                        extractedBytesMap.put(fileName, baos.toByteArray());
                    } finally {
                        baos.close();
                    }
                } else {
                    foldersMap.put(fileName, fileName);
                }
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    DataMaps dataMaps = new DataMaps();
    dataMaps.setBytesMap(extractedBytesMap);
    dataMaps.setFoldersMap(foldersMap);
    return dataMaps;
}

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 www.  j a  va 2  s  .  com*/
    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:com.joliciel.talismane.machineLearning.linearsvm.LinearSVMOneVsRestModel.java

@Override
public void loadModelFromStream(InputStream inputStream) {
    // load model or use it directly
    try {/*from w  w w.  ja v a 2 s.  c om*/
        models = new ArrayList<Model>();
        ZipInputStream zis = new ZipInputStream(inputStream);
        ZipEntry zipEntry = null;
        while ((zipEntry = zis.getNextEntry()) != null) {
            LOG.debug("Reading " + zipEntry.getName());
            Reader reader = new InputStreamReader(zis, "UTF-8");
            Model model = Model.load(reader);
            models.add(model);
        }
    } catch (UnsupportedEncodingException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    }
}

From source file:ch.rgw.tools.StringTool.java

/**
 * Ein mit flatten() erzeugtes Byte-Array wieder in eine HAshtable zurckverwandeln
 * //from  w  w  w  . j a v  a  2  s. co  m
 * @param flat
 *            Die komprimierte Hashtable
 * @param compressMode
 *            Expnad-Modus
 * @param ExtInfo
 * @return die Hastbale
 */
@SuppressWarnings("unchecked")
@Deprecated
public static Hashtable fold(final byte[] flat, final int compressMode, final Object ExtInfo) {
    ObjectInputStream ois = null;
    try {
        ByteArrayInputStream bais = new ByteArrayInputStream(flat);
        switch (compressMode) {
        case BZIP:
            ois = new ObjectInputStream(new CBZip2InputStream(bais));
            break;
        case HUFF:
            ois = new ObjectInputStream(new HuffmanInputStream(bais));
            break;
        case GLZ:
            ois = new ObjectInputStream(new GLZInputStream(bais));
            break;
        case ZIP:
            ZipInputStream zis = new ZipInputStream(bais);
            zis.getNextEntry();
            ois = new ObjectInputStream(zis);
            break;
        case GUESS:
            Hashtable<Object, Object> res = fold(flat, ZIP, null);
            if (res == null) {
                res = fold(flat, GLZ, null);
                if (res == null) {
                    res = fold(flat, BZIP, null);
                    if (res == null) {
                        res = fold(flat, HUFF, ExtInfo);
                        if (res == null) {
                            return null;
                        }
                    }
                }
            }
            return res;
        default:
            ois = new ObjectInputStream(bais);
            break;
        }

        Hashtable<Object, Object> res = (Hashtable<Object, Object>) ois.readObject();
        ois.close();
        bais.close();
        return res;
    } catch (Exception ex) {
        // ExHandler.handle(ex); deliberately don't mind
        return null;
    }

}

From source file:com.graphhopper.reader.OSMInputFile.java

@SuppressWarnings("unchecked")
private InputStream decode(File file) throws IOException {
    final String name = file.getName();

    InputStream ips = null;//from w  w w  .jav  a  2  s  .  c o m
    try {
        ips = new BufferedInputStream(new FileInputStream(file), 50000);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    ips.mark(10);

    // check file header
    byte header[] = new byte[6];
    ips.read(header);

    /*     can parse bz2 directly with additional lib
     if (header[0] == 'B' && header[1] == 'Z')
     {
     return new CBZip2InputStream(ips);
     }
     */
    if (header[0] == 31 && header[1] == -117) {
        ips.reset();
        return new GZIPInputStream(ips, 50000);
    } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9
            && (header[3] == 13 || header[3] == 14)) {
        ips.reset();
        binary = true;
        return ips;
    } else if (header[0] == 'P' && header[1] == 'K') {
        ips.reset();
        ZipInputStream zip = new ZipInputStream(ips);
        zip.getNextEntry();

        return zip;
    } else if (name.endsWith(".osm") || name.endsWith(".xml")) {
        ips.reset();
        return ips;
    } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) {
        String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream";
        try {
            Class clazz = Class.forName(clName);
            ips.reset();
            Constructor<InputStream> ctor = clazz.getConstructor(InputStream.class, boolean.class);
            return ctor.newInstance(ips, true);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot instantiate " + clName, e);
        }
    } else {
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());
    }
}

From source file:com.graphhopper.reader.osm.OSMInputFile.java

@SuppressWarnings("unchecked")
private InputStream decode(File file) throws IOException {
    final String name = file.getName();

    InputStream ips = null;/*from   w w w. j ava2 s .  co  m*/
    try {
        ips = new BufferedInputStream(new FileInputStream(file), 50000);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    ips.mark(10);

    // check file header
    byte header[] = new byte[6];
    if (ips.read(header) < 0)
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());

    /*     can parse bz2 directly with additional lib
     if (header[0] == 'B' && header[1] == 'Z')
     {
     return new CBZip2InputStream(ips);
     }
     */
    if (header[0] == 31 && header[1] == -117) {
        ips.reset();
        return new GZIPInputStream(ips, 50000);
    } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9
            && (header[3] == 13 || header[3] == 14)) {
        ips.reset();
        binary = true;
        return ips;
    } else if (header[0] == 'P' && header[1] == 'K') {
        ips.reset();
        ZipInputStream zip = new ZipInputStream(ips);
        zip.getNextEntry();

        return zip;
    } else if (name.endsWith(".osm") || name.endsWith(".xml")) {
        ips.reset();
        return ips;
    } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) {
        String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream";
        try {
            Class clazz = Class.forName(clName);
            ips.reset();
            Constructor<InputStream> ctor = clazz.getConstructor(InputStream.class, boolean.class);
            return ctor.newInstance(ips, true);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot instantiate " + clName, e);
        }
    } else {
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());
    }
}

From source file:com.marklogic.contentpump.ArchiveRecordReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    if (zipIn == null) {
        hasNext = false;/*  www.ja va  2 s.co  m*/
        return false;
    }

    ZipEntry zipEntry;
    ZipInputStream zis = (ZipInputStream) zipIn;
    if (value == null) {
        value = new DatabaseDocumentWithMeta();
    }
    while ((zipEntry = zis.getNextEntry()) != null) {
        subId = zipEntry.getName();
        long length = zipEntry.getSize();
        if (subId.endsWith(DocumentMetadata.NAKED)) {
            ((DatabaseDocumentWithMeta) value).setMeta(getMetadataFromStream(length));
            String uri = subId.substring(0, subId.length() - DocumentMetadata.NAKED.length());
            setKey(uri, 0, 0, false);
            value.setContent(null);
            count++;
            return true;
        }
        if (count % 2 == 0 && subId.endsWith(DocumentMetadata.EXTENSION)) {
            ((DatabaseDocumentWithMeta) value).setMeta(getMetadataFromStream(length));
            count++;
            continue;
        }
        // no meta data
        if (count % 2 == 0 && !allowEmptyMeta) {
            setSkipKey(0, 0, "Missing metadata");
            return true;
        } else {
            setKey(subId, 0, 0, false);
            readDocFromStream(length, (DatabaseDocument) value);
            count++;
            return true;
        }
    }
    //end of a zip
    if (iterator != null && iterator.hasNext()) {
        close();
        initStream(iterator.next());
        return nextKeyValue();
    } else {
        hasNext = false;
        return false;
    }
}

From source file:com.marklogic.contentpump.CompressedRDFReader.java

@Override
public boolean nextKeyValue() throws IOException, InterruptedException {
    boolean stillReading = super.nextKeyValue();
    if (stillReading) {
        return true;
    }/*from  w w  w  .  j  ava  2s .c  o  m*/

    // Ok, we've run out of data in the current file, are there more?
    URI zipURI = file.toUri();
    if (codec.equals(CompressionCodec.ZIP)) {
        ZipInputStream zis = (ZipInputStream) zipIn;

        ByteArrayOutputStream baos;
        while ((currZipEntry = zis.getNextEntry()) != null) {
            if (currZipEntry.getSize() == 0) {
                continue;
            }

            long size = currZipEntry.getSize();
            if (size == -1) {
                baos = new ByteArrayOutputStream();
                // if we don't know the size, assume it's big!
                initParser(zipURI.toASCIIString() + "/" + currZipEntry.getName(), INMEMORYTHRESHOLD);
            } else {
                baos = new ByteArrayOutputStream((int) size);
                initParser(zipURI.toASCIIString() + "/" + currZipEntry.getName(), size);
            }
            int nb;
            while ((nb = zis.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, nb);
            }

            parse(currZipEntry.getName(), new ByteArrayInputStream(baos.toByteArray()));
            boolean gotTriples = super.nextKeyValue();
            if (gotTriples) {
                return true;
            }
        }
        // end of zip
        if (iterator != null && iterator.hasNext()) {
            close();
            initStream(iterator.next());
            return super.nextKeyValue();
        }

        return false;
    } else {
        return false;
    }
}