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.joliciel.csvLearner.CSVEventListReader.java

/**
 * Scan a feature directory and all of its sub-directories, and add the
 * contents of the feature files to the event map.
 * //from w ww . j av a2 s  . c  o  m
 * @param featureDir
 * @throws IOException
 */
void scanFeatureDir(File featureDir, boolean grouped) throws IOException {
    LOG.debug("Scanning feature directory " + featureDir.getPath());
    File[] files = featureDir.listFiles();
    if (files == null) {
        LOG.debug("Not a directory!");
        return;
    }
    for (File file : files) {
        if (file.isDirectory()) {
            // recursively scan this feature sub-directory
            this.scanFeatureDir(file, grouped);
        } else {
            String fileName = file.getName();
            LOG.debug("Scanning file " + fileName);
            Map<String, GenericEvent> currentEventMap = eventMap;
            if (eventFileMap != null) {
                currentEventMap = new TreeMap<String, GenericEvent>();
                // copy the results to the event map
                for (GenericEvent event : eventMap.values()) {
                    GenericEvent eventClone = new GenericEvent(event.getIdentifier());
                    eventClone.setTest(event.isTest());
                    eventClone.setOutcome(event.getOutcome());
                    currentEventMap.put(event.getIdentifier(), eventClone);
                }
                eventFileMap.put(fileName, currentEventMap);
            }
            InputStream inputStream = null;
            try {
                if (fileName.endsWith(".dsc_limits.csv") || fileName.endsWith(".nrm_limits.csv")) {
                    LOG.trace("Ignoring limits file: " + fileName);
                } else if (fileName.endsWith(".csv")) {
                    inputStream = new FileInputStream(file);
                    this.scanCSVFile(inputStream, true, grouped, fileName, currentEventMap);
                } else if (fileName.endsWith(".zip")) {
                    inputStream = new FileInputStream(file);
                    ZipInputStream zis = new ZipInputStream(inputStream);
                    ZipEntry zipEntry;
                    while ((zipEntry = zis.getNextEntry()) != null) {
                        LOG.debug("Scanning zip entry " + zipEntry.getName());

                        this.scanCSVFile(zis, false, grouped, fileName, currentEventMap);
                        zis.closeEntry();
                    }

                    zis.close();
                } else {
                    throw new RuntimeException("Bad file extension in feature directory: " + file.getName());
                }
            } finally {
                if (inputStream != null)
                    inputStream.close();
            }
        } // file or directory?
    } // next file
}

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

private static void mergeEntries(ZipOutputStream zip, File file, Set<String> saw) throws IOException {
    try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)))) {
        while (true) {
            ZipEntry entry = in.getNextEntry();
            if (entry == null) {
                break;
            }/*from   w  ww  . j  ava  2s  . c o m*/
            if (saw.contains(entry.getName())) {
                continue;
            }
            if (LOG.isTraceEnabled()) {
                LOG.trace("Copy into archive: {} -> {}", entry.getName(), file);
            }
            saw.add(entry.getName());
            zip.putNextEntry(new ZipEntry(entry.getName()));
            copyStream(in, zip);
        }
    }
}

From source file:lucee.commons.io.compress.CompressUtil.java

private static void listZip(Resource zipFile) throws IOException {
    if (!zipFile.exists())
        throw new IOException(zipFile + " is not a existing file");

    if (zipFile.isDirectory()) {
        throw new IOException(zipFile + " is a directory");
    }//ww w. j a v a 2  s .c om

    ZipInputStream zis = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(zipFile.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (!entry.isDirectory()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtil.copy(zis, baos, false, false);
                byte[] barr = baos.toByteArray();
                aprint.o(entry.getName() + ":" + barr.length);
            }
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}

From source file:eu.europa.esig.dss.asic.validation.ASiCContainerValidator.java

private void analyseEntries() throws DSSException {

    ZipInputStream asicsInputStream = null;
    try {//from w  w  w .  j a v a2 s.co m

        MimeType asicEntryMimeType = null;
        asicsInputStream = new ZipInputStream(asicContainer.openStream()); // The underlying stream is closed by the parent (asicsInputStream).

        for (ZipEntry entry = asicsInputStream.getNextEntry(); entry != null; entry = asicsInputStream
                .getNextEntry()) {

            String entryName = entry.getName();
            if (isCAdES(entryName)) {

                if (xadesSigned) {
                    throw new DSSNotETSICompliantException(
                            DSSNotETSICompliantException.MSG.DIFFERENT_SIGNATURE_FORMATS);
                }
                addEntryElement(entryName, signatures, asicsInputStream);
                cadesSigned = true;
            } else if (isXAdES(entryName)) {

                if (cadesSigned) {
                    throw new DSSNotETSICompliantException(
                            DSSNotETSICompliantException.MSG.DIFFERENT_SIGNATURE_FORMATS);
                }
                addEntryElement(entryName, signatures, asicsInputStream);
                xadesSigned = true;
            } else if (isTimestamp(entryName)) {

                addEntryElement(entryName, signatures, asicsInputStream);
                timestamped = true;
            } else if (isASiCManifest(entryName)) {

                addAsicManifestEntryElement(entryName, detachedContents, asicsInputStream);
            } else if (isManifest(entryName)) {

                addEntryElement(entryName, detachedContents, asicsInputStream);
            } else if (isContainer(entryName)) {

                addEntryElement(entryName, detachedContents, asicsInputStream);
            } else if (isMetadata(entryName)) {

                addEntryElement(entryName, detachedContents, asicsInputStream);
            } else if (isMimetype(entryName)) {

                final DSSDocument mimeType = addEntryElement(entryName, detachedContents, asicsInputStream);
                asicEntryMimeType = getMimeType(mimeType);
            } else if (entryName.indexOf("/") == -1) {

                addEntryElement(entryName, detachedContents, asicsInputStream);
            } else if (entryName.endsWith("/")) { // Folder
                continue;
            } else {

                addEntryElement(entryName, detachedContents, asicsInputStream);
            }
        }
        asicMimeType = determinateAsicMimeType(asicContainer.getMimeType(), asicEntryMimeType);
        if (MimeType.ASICS == asicMimeType) {

            final ListIterator<DSSDocument> dssDocumentListIterator = detachedContents.listIterator();
            while (dssDocumentListIterator.hasNext()) {

                final DSSDocument dssDocument = dssDocumentListIterator.next();
                final String detachedContentName = dssDocument.getName();
                if ("mimetype".equals(detachedContentName)) {
                    dssDocumentListIterator.remove();
                } else if (detachedContentName.indexOf('/') != -1) {
                    dssDocumentListIterator.remove();
                }
            }
        }
    } catch (Exception e) {
        if (e instanceof DSSException) {
            throw (DSSException) e;
        }
        throw new DSSException(e);
    } finally {
        IOUtils.closeQuietly(asicsInputStream);
    }
}

From source file:org.atomspace.pi2c.runtime.Server.java

/**
 * Loading Jars with the ClassLoader and Unzip the included Jar-Files from libs folder
 * @throws SecurityException/*  w  w w. ja v  a 2 s.c  om*/
 * @throws NoSuchMethodException
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private void loadingJars() throws SecurityException, NoSuchMethodException, IOException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // Prepair Classloader Reflextion
    Method m = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class });
    m.setAccessible(true);

    ClassLoader cl = Server.class.getClassLoader();

    // Create Temp Dir
    File appTmp = new File(System.getProperty("java.io.tmpdir") + "/"
            + location.toString().substring(location.toString().lastIndexOf("/")));
    System.err.println("::: use temporary dir path '" + appTmp.getAbsolutePath() + "'");
    if (!appTmp.exists()) {
        appTmp.mkdir();
    }

    ZipInputStream zip = new ZipInputStream(location.openStream());
    ZipEntry entry = zip.getNextEntry();
    while (entry != null) {
        if (entry.getName().startsWith("libs/") && entry.getName().endsWith(".jar")
                || (entry.getName().endsWith("MANIFEST.MF"))) {
            System.err.println("::: " + new Date() + " ::: Extracting " + entry.getName() + " :::");
            InputStream is = cl.getResourceAsStream(entry.getName());
            File tmpJar = new File(appTmp + "/" + entry.getName().substring(entry.getName().lastIndexOf("/")));
            unzip(is, tmpJar);
            m.invoke(cl, new Object[] { tmpJar.toURI().toURL() });
        } else {
            System.err.println("::: " + new Date() + " ::: ignore " + entry.getName() + " :::");

        }
        entry = zip.getNextEntry();
    }
}

From source file:net.pms.configuration.DownloadPlugins.java

private void unzip(File f, String dir) {
    // Zip file with loads of goodies
    // Unzip it//from  w  w  w .  j  a  v a  2  s  . c o  m
    ZipInputStream zis;
    try {
        zis = new ZipInputStream(new FileInputStream(f));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File dst = new File(dir + File.separator + entry.getName());
            if (entry.isDirectory()) {
                dst.mkdirs();
                continue;
            }
            int count;
            byte data[] = new byte[4096];
            FileOutputStream fos = new FileOutputStream(dst);
            try (BufferedOutputStream dest = new BufferedOutputStream(fos, 4096)) {
                while ((count = zis.read(data, 0, 4096)) != -1) {
                    dest.write(data, 0, count);
                }
                dest.flush();
            }
            if (dst.getAbsolutePath().endsWith(".jar")) {
                jars.add(dst.toURI().toURL());
            }
        }
        zis.close();
    } catch (Exception e) {
        LOGGER.info("unzip error " + e);
    }
    f.delete();
}

From source file:fr.inria.atlanmod.neoemf.benchmarks.datastore.helper.BackendHelper.java

private static List<String> getZipResources() throws IOException {
    if (isNull(AVAILABLE_RESOURCES)) {
        AVAILABLE_RESOURCES = new ArrayList<>();

        try (ZipInputStream inputStream = new ZipInputStream(
                BackendHelper.class.getResourceAsStream("/" + ZIP_FILENAME))) {
            ZipEntry entry = inputStream.getNextEntry();
            while (nonNull(entry)) {
                if (!entry.isDirectory() && checkValidResource(entry.getName())) {
                    AVAILABLE_RESOURCES.add(new File(entry.getName()).getName());
                }//  www. j  av  a  2  s.  c  o  m
                inputStream.closeEntry();
                entry = inputStream.getNextEntry();
            }
        }
    }
    return AVAILABLE_RESOURCES;
}

From source file:com.baasbox.controllers.Root.java

/**
 * /admin/db/import (POST)// w w w  .  jav a  2 s  .  c o m
 * 
 * the method allows to upload a json export file and apply it to the db.
 * WARNING: all data on the db will be wiped out before importing
 * 
 * @return a 200 Status code when the import is successfull,a 500 status code otherwise
 */
@With({ RootCredentialWrapFilter.class, ConnectToDBFilter.class })
public static Result importDb() {
    String appcode = (String) ctx().args.get("appcode");
    MultipartFormData body = request().body().asMultipartFormData();
    if (body == null)
        return badRequest("missing data: is the body multipart/form-data?");
    FilePart fp = body.getFile("file");

    if (fp != null) {
        ZipInputStream zis = null;
        try {
            java.io.File multipartFile = fp.getFile();
            java.util.UUID uuid = java.util.UUID.randomUUID();
            File zipFile = File.createTempFile(uuid.toString(), ".zip");
            FileUtils.copyFile(multipartFile, zipFile);
            zis = new ZipInputStream(new FileInputStream(zipFile));
            DbManagerService.importDb(appcode, zis);
            zipFile.delete();
            return ok();
        } catch (Exception e) {
            BaasBoxLogger.error(ExceptionUtils.getStackTrace(e));
            return internalServerError(ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (zis != null) {
                    zis.close();
                }
            } catch (IOException e) {
                // Nothing to do here
            }
        }
    } else {
        return badRequest("The form was submitted without a multipart file field.");
    }
}

From source file:com.facebook.buck.jvm.java.JarDirectoryStepTest.java

@Test
public void jarsShouldContainDirectoryEntries() throws IOException {
    Path zipup = folder.newFolder("dir-zip");

    Path subdir = zipup.resolve("dir/subdir");
    Files.createDirectories(subdir);
    Files.write(subdir.resolve("a.txt"), "cake".getBytes());

    JarDirectoryStep step = new JarDirectoryStep(new ProjectFilesystem(zipup), Paths.get("output.jar"),
            ImmutableSortedSet.of(zipup), /* main class */ null, /* manifest file */ null);
    ExecutionContext context = TestExecutionContext.newInstance();

    int returnCode = step.execute(context).getExitCode();

    assertEquals(0, returnCode);//from   www.j a va2s.co m

    Path zip = zipup.resolve("output.jar");
    assertTrue(Files.exists(zip));

    // Iterate over each of the entries, expecting to see the directory names as entries.
    Set<String> expected = Sets.newHashSet("dir/", "dir/subdir/");
    try (ZipInputStream is = new ZipInputStream(Files.newInputStream(zip))) {
        for (ZipEntry entry = is.getNextEntry(); entry != null; entry = is.getNextEntry()) {
            expected.remove(entry.getName());
        }
    }
    assertTrue("Didn't see entries for: " + expected, expected.isEmpty());
}

From source file:com.joliciel.jochre.search.JochreIndexBuilderImpl.java

private void updateDocumentInternal(File documentDir) {
    try {//from  w  w  w  . jav a 2s.c  om
        LOG.info("Updating index for " + documentDir.getName());

        File zipFile = new File(documentDir, documentDir.getName() + ".zip");
        if (!zipFile.exists()) {
            LOG.info("Nothing to index in " + documentDir.getName());
            return;
        }
        long zipDate = zipFile.lastModified();

        this.deleteDocumentInternal(documentDir);

        File[] offsetFiles = documentDir.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".obj");
            }
        });

        for (File offsetFile : offsetFiles) {
            offsetFile.delete();
        }

        int i = 0;

        Map<String, String> fields = new TreeMap<String, String>();
        File metaDataFile = new File(documentDir, "metadata.txt");
        Scanner scanner = new Scanner(
                new BufferedReader(new InputStreamReader(new FileInputStream(metaDataFile), "UTF-8")));
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            String key = line.substring(0, line.indexOf('\t'));
            String value = line.substring(line.indexOf('\t'));
            fields.put(key, value);
        }
        scanner.close();

        JochreXmlDocument xmlDoc = this.searchService.newDocument();
        JochreXmlReader reader = this.searchService.getJochreXmlReader(xmlDoc);

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = null;
        while ((ze = zis.getNextEntry()) != null) {
            LOG.debug("Adding zipEntry " + i + ": " + ze.getName());
            String baseName = ze.getName().substring(0, ze.getName().lastIndexOf('.'));
            UnclosableInputStream uis = new UnclosableInputStream(zis);
            reader.parseFile(uis, baseName);
            i++;
        }
        zis.close();

        i = 0;
        StringBuilder sb = new StringBuilder();
        coordinateStorage = searchService.getCoordinateStorage();
        offsetLetterMap = new HashMap<Integer, JochreXmlLetter>();
        int startPage = -1;
        int endPage = -1;
        int docCount = 0;
        int wordCount = 0;
        int cumulWordCount = 0;
        for (JochreXmlImage image : xmlDoc.getImages()) {
            if (startPage < 0)
                startPage = image.getPageIndex();
            endPage = image.getPageIndex();
            int remainingWords = xmlDoc.wordCount() - (cumulWordCount + wordCount);
            LOG.debug("Word count: " + wordCount + ", cumul word count: " + cumulWordCount
                    + ", total xml words: " + xmlDoc.wordCount() + ", remaining words: " + remainingWords);
            if (wordsPerDoc > 0 && wordCount >= wordsPerDoc && remainingWords >= wordsPerDoc) {
                LOG.debug("Creating new index doc: " + docCount);
                JochreIndexDocument indexDoc = searchService.newJochreIndexDocument(documentDir, docCount, sb,
                        coordinateStorage, startPage, endPage, fields);
                indexDoc.save(indexWriter);
                docCount++;

                sb = new StringBuilder();
                coordinateStorage = searchService.getCoordinateStorage();
                startPage = image.getPageIndex();
                offsetLetterMap = new HashMap<Integer, JochreXmlLetter>();
                cumulWordCount += wordCount;
                wordCount = 0;
            }

            LOG.debug("Processing page: " + image.getFileNameBase());

            File imageFile = null;
            for (String imageExtension : imageExtensions) {
                imageFile = new File(documentDir, image.getFileNameBase() + "." + imageExtension);
                if (imageFile.exists())
                    break;
                imageFile = null;
            }
            if (imageFile == null)
                throw new RuntimeException("No image found in directory " + documentDir.getAbsolutePath()
                        + ", baseName " + image.getFileNameBase());

            coordinateStorage.addImage(sb.length(), imageFile.getName(), image.getPageIndex());

            for (JochreXmlParagraph par : image.getParagraphs()) {
                coordinateStorage.addParagraph(sb.length(),
                        new Rectangle(par.getLeft(), par.getTop(), par.getRight(), par.getBottom()));
                for (JochreXmlRow row : par.getRows()) {
                    coordinateStorage.addRow(sb.length(),
                            new Rectangle(row.getLeft(), row.getTop(), row.getRight(), row.getBottom()));
                    int k = 0;
                    for (JochreXmlWord word : row.getWords()) {
                        wordCount++;
                        for (JochreXmlLetter letter : word.getLetters()) {
                            offsetLetterMap.put(sb.length(), letter);
                            if (letter.getText().length() > 1) {
                                for (int j = 1; j < letter.getText().length(); j++) {
                                    offsetLetterMap.put(sb.length() + j, letter);
                                }
                            }
                            sb.append(letter.getText());
                        }
                        k++;
                        boolean finalDash = false;
                        if (k == row.getWords().size() && word.getText().endsWith("-")
                                && word.getText().length() > 1)
                            finalDash = true;
                        if (!finalDash)
                            sb.append(" ");
                    }
                }
                sb.append("\n");
            }
            i++;
        }
        JochreIndexDocument indexDoc = searchService.newJochreIndexDocument(documentDir, docCount, sb,
                coordinateStorage, startPage, endPage, fields);
        indexDoc.save(indexWriter);

        File lastIndexDateFile = new File(documentDir, "indexDate.txt");

        Writer writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(lastIndexDateFile, false), "UTF8"));
        writer.write("" + zipDate);
        writer.flush();

        writer.close();
    } catch (IOException ioe) {
        LogUtils.logError(LOG, ioe);
        throw new RuntimeException(ioe);
    }
}