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.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 w  w w.j  a v a 2  s.  c  o  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:edu.umd.cs.marmoset.modelClasses.Project.java

public static Project importProject(InputStream in, Course course,
        StudentRegistration canonicalStudentRegistration, Connection conn)
        throws SQLException, IOException, ClassNotFoundException {
    Project project = new Project();
    ZipInputStream zipIn = new ZipInputStream(in);

    // Start transaction
    conn.setAutoCommit(false);//from  w w  w .  jav  a  2  s .co  m
    conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

    byte[] canonicalBytes = null;
    byte[] testSetupBytes = null;
    byte[] projectStarterFileBytes = null;

    while (true) {
        ZipEntry entry = zipIn.getNextEntry();
        if (entry == null)
            break;
        if (entry.getName().contains("project.out")) {
            // Found the serialized project!
            ObjectInputStream objectInputStream = new ObjectInputStream(zipIn);

            project = (Project) objectInputStream.readObject();

            // Set the PKs to null, the values that get serialized are actually from
            // a different database with a different set of keys
            project.setProjectPK(0);
            project.setTestSetupPK(0);
            project.setArchivePK(null);
            project.setVisibleToStudents(false);

            // These two PKs need to be passed in when we import/create the project
            project.setCoursePK(course.getCoursePK());
            project.setCanonicalStudentRegistrationPK(canonicalStudentRegistration.getStudentRegistrationPK());

            // Insert the project so that we have a projectPK for other methods
            project.insert(conn);

        } else if (entry.getName().contains("canonical")) {
            // Found the canonical submission...
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            CopyUtils.copy(zipIn, baos);
            canonicalBytes = baos.toByteArray();
        } else if (entry.getName().contains("test-setup")) {
            // Found the test-setup!
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            CopyUtils.copy(zipIn, baos);
            testSetupBytes = baos.toByteArray();
        } else if (entry.getName().contains("project-starter-files")) {
            // Found project starter files
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            CopyUtils.copy(zipIn, baos);
            projectStarterFileBytes = baos.toByteArray();
        }
    }

    Timestamp submissionTimestamp = new Timestamp(System.currentTimeMillis());

    // Now "upload" bytes as an archive for the project starter files, if it exists
    if (projectStarterFileBytes != null) {
        project.setArchiveForUpload(projectStarterFileBytes);
        project.uploadCachedArchive(conn);
    }

    // Now "submit" these bytes as a canonical submission
    // TODO read the submissionTimestamp from the serialized project in the archive
    Submission submission = Submission.submit(canonicalBytes, canonicalStudentRegistration, project,
            "t" + submissionTimestamp.getTime(), "ProjectImportTool, serialMinorVersion",
            Integer.toString(serialMinorVersion, 100), submissionTimestamp, conn);

    // Now "upload" the test-setup bytes as an archive
    String comment = "Project Import Tool uploaded at " + submissionTimestamp;
    TestSetup testSetup = TestSetup.submit(testSetupBytes, project, comment, conn);
    project.setTestSetupPK(testSetup.getTestSetupPK());
    testSetup.setTestRunPK(submission.getCurrentTestRunPK());

    testSetup.update(conn);

    return project;
}

From source file:ca.uhn.fhir.jpa.term.TerminologyLoaderSvc.java

private void extractFiles(List<byte[]> theZipBytes, List<String> theExpectedFilenameFragments) {
    Set<String> foundFragments = new HashSet<String>();

    for (byte[] nextZipBytes : theZipBytes) {
        ZipInputStream zis = new ZipInputStream(
                new BufferedInputStream(new ByteArrayInputStream(nextZipBytes)));
        try {/*from  ww w .j  a  v  a  2  s. co m*/
            for (ZipEntry nextEntry; (nextEntry = zis.getNextEntry()) != null;) {
                for (String next : theExpectedFilenameFragments) {
                    if (nextEntry.getName().contains(next)) {
                        foundFragments.add(next);
                    }
                }
            }
        } catch (IOException e) {
            throw new InternalErrorException(e);
        } finally {
            IOUtils.closeQuietly(zis);
        }
    }

    for (String next : theExpectedFilenameFragments) {
        if (!foundFragments.contains(next)) {
            throw new InvalidRequestException(
                    "Invalid input zip file, expected zip to contain the following name fragments: "
                            + theExpectedFilenameFragments + " but found: " + foundFragments);
        }
    }

}

From source file:ca.uhn.fhir.jpa.term.TerminologyLoaderSvc.java

private void iterateOverZipFile(List<byte[]> theZipBytes, String fileNamePart, IRecordHandler handler,
        char theDelimiter, QuoteMode theQuoteMode) {
    boolean found = false;

    for (byte[] nextZipBytes : theZipBytes) {
        ZipInputStream zis = new ZipInputStream(
                new BufferedInputStream(new ByteArrayInputStream(nextZipBytes)));
        try {//from  w ww.  ja  v a  2  s .  c om
            for (ZipEntry nextEntry; (nextEntry = zis.getNextEntry()) != null;) {
                ZippedFileInputStream inputStream = new ZippedFileInputStream(zis);

                String nextFilename = nextEntry.getName();
                if (nextFilename.contains(fileNamePart)) {
                    ourLog.info("Processing file {}", nextFilename);
                    found = true;

                    Reader reader = null;
                    CSVParser parsed = null;
                    try {
                        reader = new InputStreamReader(zis, Charsets.UTF_8);
                        CSVFormat format = CSVFormat.newFormat(theDelimiter).withFirstRecordAsHeader();
                        if (theQuoteMode != null) {
                            format = format.withQuote('"').withQuoteMode(theQuoteMode);
                        }
                        parsed = new CSVParser(reader, format);
                        Iterator<CSVRecord> iter = parsed.iterator();
                        ourLog.debug("Header map: {}", parsed.getHeaderMap());

                        int count = 0;
                        int logIncrement = LOG_INCREMENT;
                        int nextLoggedCount = 0;
                        while (iter.hasNext()) {
                            CSVRecord nextRecord = iter.next();
                            handler.accept(nextRecord);
                            count++;
                            if (count >= nextLoggedCount) {
                                ourLog.info(" * Processed {} records in {}", count, nextFilename);
                                nextLoggedCount += logIncrement;
                            }
                        }

                    } catch (IOException e) {
                        throw new InternalErrorException(e);
                    }
                }
            }
        } catch (IOException e) {
            throw new InternalErrorException(e);
        } finally {
            IOUtils.closeQuietly(zis);
        }
    }

    // This should always be true, but just in case we've introduced a bug...
    Validate.isTrue(found);
}

From source file:com.joliciel.talismane.GenericLanguageImplementation.java

public void setLanguagePack(File languagePackFile) {
    ZipInputStream zis = null;
    try {//from www  .  java  2 s  .  co  m
        zis = new ZipInputStream(new FileInputStream(languagePackFile));
        ZipEntry ze = null;

        Map<String, String> argMap = new HashMap<String, String>();
        while ((ze = zis.getNextEntry()) != null) {
            String name = ze.getName();
            if (name.indexOf('/') >= 0)
                name = name.substring(name.lastIndexOf('/') + 1);
            if (name.equals("languagePack.properties")) {
                Properties props = new Properties();
                props.load(zis);
                argMap = StringUtils.getArgMap(props);
                break;
            }
        } // next zip entry
        zis.close();

        Map<String, String> reverseMap = new HashMap<String, String>();
        for (String key : argMap.keySet()) {
            String value = argMap.get(key);
            if (value.startsWith("replace:")) {
                value = value.substring("replace:".length());
                if (key.equals("textFilters")) {
                    replaceTextFilters = true;
                } else if (key.equals("tokenFilters")) {
                    replaceTokenFilters = true;
                } else if (key.equals("tokenSequenceFilters")) {
                    replaceTokenSequenceFilters = true;
                }
            }
            if (key.equals("locale")) {
                locale = Locale.forLanguageTag(value);
            } else {
                reverseMap.put(value, key);
            }
        }

        zis = new ZipInputStream(new FileInputStream(languagePackFile));
        ze = null;

        while ((ze = zis.getNextEntry()) != null) {
            String name = ze.getName();
            if (name.indexOf('/') >= 0)
                name = name.substring(name.lastIndexOf('/') + 1);
            String key = reverseMap.get(name);
            if (key != null) {
                if (key.equals("transitionSystem")) {
                    transitionSystem = this.getParserService().getArcEagerTransitionSystem();
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    List<String> dependencyLabels = new ArrayListNoNulls<String>();
                    while (scanner.hasNextLine()) {
                        String dependencyLabel = scanner.nextLine();
                        if (!dependencyLabel.startsWith("#")) {
                            if (dependencyLabel.indexOf('\t') > 0)
                                dependencyLabel = dependencyLabel.substring(0, dependencyLabel.indexOf('\t'));
                            dependencyLabels.add(dependencyLabel);
                        }
                    }
                    transitionSystem.setDependencyLabels(dependencyLabels);
                } else if (key.equals("posTagSet")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    posTagSet = this.getPosTaggerService().getPosTagSet(scanner);
                } else if (key.equals("textFilters")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    textFiltersStr = this.getStringFromScanner(scanner);
                } else if (key.equals("tokenFilters")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    tokenFiltersStr = this.getStringFromScanner(scanner);
                } else if (key.equals("tokenSequenceFilters")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    tokenSequenceFiltersStr = this.getStringFromScanner(scanner);
                } else if (key.equals("posTaggerRules")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    posTaggerRulesStr = this.getStringFromScanner(scanner);
                } else if (key.equals("parserRules")) {
                    Scanner scanner = new Scanner(zis, "UTF-8");
                    parserRulesStr = this.getStringFromScanner(scanner);
                } else if (key.equals("sentenceModel")) {
                    ZipInputStream innerZis = new ZipInputStream(new UnclosableInputStream(zis));
                    sentenceModel = this.getMachineLearningService().getClassificationModel(innerZis);
                } else if (key.equals("tokeniserModel")) {
                    ZipInputStream innerZis = new ZipInputStream(new UnclosableInputStream(zis));
                    tokeniserModel = this.getMachineLearningService().getClassificationModel(innerZis);
                } else if (key.equals("posTaggerModel")) {
                    ZipInputStream innerZis = new ZipInputStream(new UnclosableInputStream(zis));
                    posTaggerModel = this.getMachineLearningService().getClassificationModel(innerZis);
                } else if (key.equals("parserModel")) {
                    ZipInputStream innerZis = new ZipInputStream(new UnclosableInputStream(zis));
                    parserModel = this.getMachineLearningService().getClassificationModel(innerZis);
                } else if (key.equals("lexicon")) {
                    ZipInputStream innerZis = new ZipInputStream(new UnclosableInputStream(zis));
                    LexiconDeserializer deserializer = new LexiconDeserializer(this.getTalismaneSession());
                    lexicons = deserializer.deserializeLexicons(innerZis);
                } else if (key.equals("corpusLexiconEntryRegex")) {
                    Scanner corpusLexicalEntryRegexScanner = new Scanner(zis, "UTF-8");
                    corpusLexicalEntryReader = new RegexLexicalEntryReader(corpusLexicalEntryRegexScanner);
                } else {
                    throw new TalismaneException("Unknown key in languagePack.properties: " + key);
                }
            }
        }
    } catch (FileNotFoundException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LogUtils.logError(LOG, e);
        throw new RuntimeException(e);
    } finally {
        if (zis != null) {
            try {
                zis.close();
            } catch (IOException e) {
                LogUtils.logError(LOG, e);
                throw new RuntimeException(e);
            }
        }
    }
}

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  www  . ja v a2 s .c om
 * @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:gov.pnnl.goss.gridappsd.app.AppManagerImpl.java

@Override
public void registerApp(AppInfo appInfo, byte[] appPackage) throws Exception {
    System.out.println("REGISTER REQUEST RECEIVED ");
    // appPackage should be received as a zip file. extract this to the apps
    // directory, and write appinfo to a json file under config/
    File appHomeDir = getAppConfigDirectory();
    if (!appHomeDir.exists()) {
        appHomeDir.mkdirs();/*  www.  jav a  2s. c om*/
        if (!appHomeDir.exists()) {
            // if it still doesn't exist throw an error
            throw new Exception(
                    "App home directory does not exist and cannot be created " + appHomeDir.getAbsolutePath());
        }
    }
    System.out.println("HOME DIR " + appHomeDir.getAbsolutePath());
    // create a directory for the app
    File newAppDir = new File(appHomeDir.getAbsolutePath() + File.separator + appInfo.getId());
    if (newAppDir.exists()) {
        throw new Exception("App " + appInfo.getId() + " already exists");
    }

    try {
        newAppDir.mkdirs();
        // Extract zip file into new app dir
        ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(appPackage));
        ZipEntry entry = zipIn.getNextEntry();
        // iterates over entries in the zip file and write to dir
        while (entry != null) {
            String filePath = newAppDir + File.separator + entry.getName();
            if (!entry.isDirectory()) {
                // if the entry is a file, extracts it
                extractFile(zipIn, filePath);
            } else {
                // if the entry is a directory, make the directory
                File dir = new File(filePath);
                dir.mkdir();
            }
            zipIn.closeEntry();
            entry = zipIn.getNextEntry();
        }
        zipIn.close();

        writeAppInfo(appInfo);

        // add to apps hashmap
        apps.put(appInfo.getId(), appInfo);
    } catch (Exception e) {
        // Clean up app dir if fails
        newAppDir.delete();
        throw e;
    }
}

From source file:com.cisco.ca.cstg.pdi.services.ConfigurationServiceImpl.java

private void copyZipFile(File file, String path) throws FileNotFoundException, IOException {
    ZipInputStream zipIs = null;
    ZipEntry zEntry = null;//from   w  w  w .j ava  2 s .c  om
    FileInputStream fis = null;

    try {
        fis = new FileInputStream(file);
        zipIs = new ZipInputStream(new BufferedInputStream(fis));

        while ((zEntry = zipIs.getNextEntry()) != null) {
            byte[] tmp = new byte[4 * 1024];
            File newFile = new File(zEntry.getName());
            String directory = newFile.getParent();
            if (directory == null) {
                try (FileOutputStream fos = new FileOutputStream(path + File.separator + zEntry.getName())) {
                    int size = 0;
                    while ((size = zipIs.read(tmp)) != -1) {
                        fos.write(tmp, 0, size);
                        fos.flush();
                    }
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error occured while trying to copy zip file.", e);
        throw e;
    } finally {
        if (zipIs != null) {
            zipIs.close();
        }
        if (fis != null) {
            fis.close();
        }
        file.delete();
    }
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureFacet.java

private List<String> getRelsEntryNames() throws IOException {
    List<String> relsEntryNames = new LinkedList<String>();
    URL ooxmlUrl = this.signatureService.getOfficeOpenXMLDocumentURL();
    InputStream inputStream = ooxmlUrl.openStream();
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;/*from  w ww.j av a2s  . c o m*/
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        String zipEntryName = zipEntry.getName();
        if (false == zipEntryName.endsWith(".rels")) {
            continue;
        }
        relsEntryNames.add(zipEntryName);
    }
    return relsEntryNames;
}

From source file:be.fedict.eid.applet.service.signer.ooxml.OOXMLSignatureFacet.java

protected Document findDocument(String zipEntryName)
        throws IOException, ParserConfigurationException, SAXException {
    URL ooxmlUrl = this.signatureService.getOfficeOpenXMLDocumentURL();
    InputStream inputStream = ooxmlUrl.openStream();
    ZipInputStream zipInputStream = new ZipInputStream(inputStream);
    ZipEntry zipEntry;//from ww w .  j av a 2 s . c  o  m
    while (null != (zipEntry = zipInputStream.getNextEntry())) {
        if (false == zipEntryName.equals(zipEntry.getName())) {
            continue;
        }
        Document document = loadDocument(zipInputStream);
        return document;
    }
    return null;
}