Example usage for java.util.zip ZipFile ZipFile

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

Introduction

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

Prototype

public ZipFile(File file) throws ZipException, IOException 

Source Link

Document

Opens a ZIP file for reading given the specified File object.

Usage

From source file:azkaban.web.JobManagerServlet.java

private File unzipFile(FileItem item) throws ServletException, IOException {
    File temp = File.createTempFile("job-temp", ".zip");
    temp.deleteOnExit();//from   w w w. java 2 s.  co m
    OutputStream out = new BufferedOutputStream(new FileOutputStream(temp));
    IOUtils.copy(item.getInputStream(), out);
    out.close();
    ZipFile zipfile = new ZipFile(temp);
    File unzipped = Utils.createTempDir(new File(_tempDir));
    Utils.unzip(zipfile, unzipped);
    temp.delete();
    return unzipped;
}

From source file:com.acciente.commons.loader.ClassFinder.java

private static Set findInJar(File oJarFile, String sPackageName, Pattern oClassNamePattern, Set oClassNameSet)
        throws IOException {
    ZipFile oZipFile;//from   ww w.  jav  a  2  s.c  om
    Enumeration oZipFileEntries;

    oZipFile = new ZipFile(oJarFile);
    oZipFileEntries = oZipFile.entries();

    while (oZipFileEntries.hasMoreElements()) {
        ZipEntry oZipFileEntry = (ZipEntry) oZipFileEntries.nextElement();
        String sClassname = getFQClassname(oZipFileEntry);

        if (!oZipFileEntry.isDirectory() && sClassname.startsWith(sPackageName)) {
            if (oClassNamePattern.matcher(sClassname).matches()) {
                oClassNameSet.add(sClassname);
            }
        }
    }

    return oClassNameSet;
}

From source file:net.sourceforge.dita4publishers.tools.DitaDxpHelperTest.java

public void xtestZipMapBos() throws Exception {
    MapBosProcessorOptions options = new DitaDxpOptions();
    File outputZipFile = File.createTempFile("textZipMapBos", ".dxp");
    DitaDxpHelper.zipMapBos(mapBos, outputZipFile, options);
    assertTrue("DXP file doesn't exist", outputZipFile.exists());
    ZipFile zipFile = new ZipFile(outputZipFile);
    List<ZipEntry> entries = new ArrayList<ZipEntry>();

    Enumeration<? extends ZipEntry> enumer = zipFile.entries();
    while (enumer.hasMoreElements()) {
        entries.add(enumer.nextElement());
    }/*  w  ww  .j  av a  2 s.c om*/

    assertEquals(11, entries.size());

    // Put more checks here.

    outputZipFile.deleteOnExit();
}

From source file:com.simiacryptus.mindseye.test.unit.SerializationTest.java

@Nullable
@Override/*from w  w  w.  ja va2  s. c  o  m*/
public ToleranceStatistics test(@Nonnull final NotebookOutput log, @Nonnull final Layer layer,
        final Tensor... inputPrototype) {
    log.h1("Serialization");
    log.p("This apply will demonstrate the key's JSON serialization, and verify deserialization integrity.");

    String prettyPrint = "";
    log.h2("Raw Json");
    try {
        prettyPrint = log.eval(() -> {
            final JsonObject json = layer.getJson();
            @Nonnull
            final Layer echo = Layer.fromJson(json);
            if (echo == null)
                throw new AssertionError("Failed to deserialize");
            if (layer == echo)
                throw new AssertionError("Serialization did not copy");
            if (!layer.equals(echo))
                throw new AssertionError("Serialization not equal");
            echo.freeRef();
            return new GsonBuilder().setPrettyPrinting().create().toJson(json);
        });
        @Nonnull
        String filename = layer.getClass().getSimpleName() + "_" + log.getName() + ".json";
        log.p(log.file(prettyPrint, filename,
                String.format("Wrote Model to %s; %s characters", filename, prettyPrint.length())));
    } catch (RuntimeException e) {
        e.printStackTrace();
        Util.sleep(1000);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
        Util.sleep(1000);
    }
    log.p("");
    @Nonnull
    Object outSync = new Object();
    if (prettyPrint.isEmpty() || prettyPrint.length() > 1024 * 64)
        Arrays.stream(SerialPrecision.values()).parallel().forEach(precision -> {
            try {
                @Nonnull
                File file = new File(log.getResourceDir(), log.getName() + "_" + precision.name() + ".zip");
                layer.writeZip(file, precision);
                @Nonnull
                final Layer echo = Layer.fromZip(new ZipFile(file));
                getModels().put(precision, echo);
                synchronized (outSync) {
                    log.h2(String.format("Zipfile %s", precision.name()));
                    log.p(log.link(file, String.format("Wrote Model apply %s precision to %s; %.3fMiB bytes",
                            precision, file.getName(), file.length() * 1.0 / (0x100000))));
                }
                if (!isPersist())
                    file.delete();
                if (echo == null)
                    throw new AssertionError("Failed to deserialize");
                if (layer == echo)
                    throw new AssertionError("Serialization did not copy");
                if (!layer.equals(echo))
                    throw new AssertionError("Serialization not equal");
            } catch (RuntimeException e) {
                e.printStackTrace();
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            } catch (ZipException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    return null;
}

From source file:org.loklak.geo.GeoNames.java

public GeoNames(final File cities1000_zip, final File iso3166json, long minPopulation) throws IOException {

    // load iso3166 info
    this.iso3166toCountry = new HashMap<>();
    try {//from w w  w  . java  2 s  .  c o  m
        //String jsonString = new String(Files.readAllBytes(iso3166json.toPath()), StandardCharsets.UTF_8);
        ObjectMapper jsonMapper = new ObjectMapper(DAO.jsonFactory);
        JsonNode j = jsonMapper.readTree(iso3166json);
        for (JsonNode n : j) {
            // contains name,alpha-2,alpha-3,country-code,iso_3166-2,region-code,sub-region-code
            String name = n.get("name").textValue();
            String cc = n.get("alpha-2").textValue();
            this.iso3166toCountry.put(cc, name);
        }
    } catch (IOException e) {
        this.iso3166toCountry = new HashMap<String, String>();
    }

    // this is a processing of the cities1000.zip file from http://download.geonames.org/export/dump/

    this.id2loc = new HashMap<>();
    this.hash2ids = new HashMap<>();
    this.stopwordHashes = new HashSet<>();
    this.countryCenter = new HashMap<>();
    Map<String, CountryBounds> countryBounds = new HashMap<>();

    if (cities1000_zip == null || !cities1000_zip.exists()) {
        throw new IOException("GeoNames: file does not exist!");
    }
    ZipFile zf = null;
    BufferedReader reader = null;
    try {
        zf = new ZipFile(cities1000_zip);
        String entryName = cities1000_zip.getName();
        entryName = entryName.substring(0, entryName.length() - 3) + "txt";
        final ZipEntry ze = zf.getEntry(entryName);
        final InputStream is = zf.getInputStream(ze);
        reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
    } catch (final IOException e) {
        throw new IOException("GeoNames: Error when decompressing cities1000.zip!", e);
    }

    /* parse this fields:
    ---------------------------------------------------
    00 geonameid         : integer id of record in geonames database
    01 name              : name of geographical point (utf8) varchar(200)
    02 asciiname         : name of geographical point in plain ascii characters, varchar(200)
    03 alternatenames    : alternatenames, comma separated varchar(5000)
    04 latitude          : latitude in decimal degrees (wgs84)
    05 longitude         : longitude in decimal degrees (wgs84)
    06 feature class     : see http://www.geonames.org/export/codes.html, char(1)
    07 feature code      : see http://www.geonames.org/export/codes.html, varchar(10)
    08 country code      : ISO-3166 2-letter country code, 2 characters
    09 cc2               : alternate country codes, comma separated, ISO-3166 2-letter country code, 60 characters
    10 admin1 code       : fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)
    11 admin2 code       : code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
    12 admin3 code       : code for third level administrative division, varchar(20)
    13 admin4 code       : code for fourth level administrative division, varchar(20)
    14 population        : bigint (8 byte int)
    15 elevation         : in meters, integer
    16 dem               : digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat.
    17 timezone          : the timezone id (see file timeZone.txt) varchar(40)
    18 modification date : date of last modification in yyyy-MM-dd format
    */
    try {
        String line;
        String[] fields;
        while ((line = reader.readLine()) != null) {
            if (line.isEmpty()) {
                continue;
            }
            fields = CommonPattern.TAB.split(line);
            final long population = Long.parseLong(fields[14]);
            if (minPopulation > 0 && population < minPopulation)
                continue;
            final int geonameid = Integer.parseInt(fields[0]);
            Set<String> locnames = new LinkedHashSet<>();
            locnames.add(fields[1]);
            locnames.add(fields[2]);
            for (final String s : CommonPattern.COMMA.split(fields[3]))
                locnames.add(s);
            ArrayList<String> locnamess = new ArrayList<>(locnames.size());
            locnamess.addAll(locnames);
            String cc = fields[8]; //ISO-3166

            final GeoLocation geoLocation = new GeoLocation(Float.parseFloat(fields[4]),
                    Float.parseFloat(fields[5]), locnamess, cc);
            geoLocation.setPopulation(population);
            this.id2loc.put(geonameid, geoLocation);
            for (final String name : locnames) {
                if (name.length() < 4)
                    continue;
                String normalized = normalize(name);
                int lochash = normalized.hashCode();
                List<Integer> locs = this.hash2ids.get(lochash);
                if (locs == null) {
                    locs = new ArrayList<Integer>(1);
                    this.hash2ids.put(lochash, locs);
                }
                if (!locs.contains(geonameid))
                    locs.add(geonameid);
            }

            // update the country bounds
            CountryBounds bounds = countryBounds.get(cc);
            if (bounds == null) {
                bounds = new CountryBounds();
                countryBounds.put(cc, bounds);
            }
            bounds.extend(geoLocation);
        }
        if (reader != null)
            reader.close();
        if (zf != null)
            zf.close();
    } catch (final IOException e) {
    }

    // calculate the center of the countries
    for (Map.Entry<String, CountryBounds> country : countryBounds.entrySet()) {
        this.countryCenter.put(country.getKey(),
                new double[] { (country.getValue().lon_west - country.getValue().lon_east) / 2.0,
                        (country.getValue().lat_north - country.getValue().lat_south) / 2.0 }); // [longitude, latitude]
    }

    // finally create a statistic which names appear very often to have fill-word heuristic
    TreeMap<Integer, Set<Integer>> stat = new TreeMap<>(); // a mapping from number of occurrences of location name hashes to a set of location name hashes
    for (Map.Entry<Integer, List<Integer>> entry : this.hash2ids.entrySet()) {
        int occurrences = entry.getValue().size();
        Set<Integer> hashes = stat.get(occurrences);
        if (hashes == null) {
            hashes = new HashSet<Integer>();
            stat.put(occurrences, hashes);
        }
        hashes.add(entry.getKey());
    }
    // we consider 3/4 of this list as fill-word (approx 300): those with the most occurrences
    int good = stat.size() / 4;
    Iterator<Map.Entry<Integer, Set<Integer>>> i = stat.entrySet().iterator();
    for (int j = 0; j < good; j++)
        i.next(); // 'eat away' the good entries.
    while (i.hasNext()) {
        Set<Integer> morehashes = i.next().getValue();
        this.stopwordHashes.addAll(morehashes);
    }
}

From source file:org.openmrs.module.clinicalsummary.io.UploadSummariesTask.java

/**
 * Method that will be called to process the summary collection file. The upload process will unpack the zipped collection of summary files and then
 * decrypt them./*from   ww w  .ja  va 2  s  .c  om*/
 *
 * @throws Exception
 */
protected void processSummaries() throws Exception {
    String zipFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ZIP), ".");
    String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED),
            ".");

    File file = new File(TaskUtils.getZippedOutputPath(), zipFilename);
    OutputStream outStream = new BufferedOutputStream(new FileOutputStream(file));
    ZipFile encryptedFile = new ZipFile(new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename));

    Enumeration<? extends ZipEntry> entries = encryptedFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        String zipEntryName = zipEntry.getName();
        if (!zipEntryName.endsWith(TaskConstants.FILE_TYPE_SAMPLE)
                && !zipEntryName.endsWith(TaskConstants.FILE_TYPE_SECRET)) {
            int count;
            byte[] data = new byte[TaskConstants.BUFFER_SIZE];
            CipherInputStream zipCipherInputStream = new CipherInputStream(
                    encryptedFile.getInputStream(zipEntry), cipher);
            while ((count = zipCipherInputStream.read(data, 0, TaskConstants.BUFFER_SIZE)) != -1) {
                outStream.write(data, 0, count);
            }
            zipCipherInputStream.close();
        }
    }
    outStream.close();

    File outputPath = TaskUtils.getSummaryOutputPath();
    ZipFile zipFile = new ZipFile(file);
    Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
    while (zipEntries.hasMoreElements()) {
        ZipEntry zipEntry = zipEntries.nextElement();
        processedFilename = zipEntry.getName();
        File f = new File(outputPath, zipEntry.getName());
        // ensure that the parent path exists
        File parent = f.getParentFile();
        if (parent.exists() || parent.mkdirs()) {
            FileCopyUtils.copy(zipFile.getInputStream(zipEntry),
                    new BufferedOutputStream(new FileOutputStream(f)));
        }
    }
}

From source file:ZipExploder.java

/**
 * Explode source ZIP or JAR file into a target directory
 * /*from  w w  w  .  jav a  2 s . c  o m*/
 * @param zipName
 *          names of source file
 * @param destDir
 *          target directory name (should already exist)
 * @exception IOException
 *              error creating a target file
 */
public void processFile(String zipName, String destDir) throws IOException {
    String source = new File(zipName).getCanonicalPath();
    String dest = new File(destDir).getCanonicalPath();
    ZipFile f = null;
    try {
        f = new ZipFile(source);
        Map fEntries = getEntries(f);
        String[] names = (String[]) fEntries.keySet().toArray(new String[] {});
        if (sortNames) {
            Arrays.sort(names);
        }
        // copy all files
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            ZipEntry e = (ZipEntry) fEntries.get(name);
            copyFileEntry(dest, f, e);
        }
    } catch (IOException ioe) {
        String msg = ioe.getMessage();
        if (msg.indexOf(zipName) < 0) {
            msg += " - " + zipName;
        }
        throw new IOException(msg);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:de.uni_potsdam.hpi.bpt.promnicat.importer.bpmai.BpmaiImporter.java

/**
 * Scans the given root directory for sgx-archives and extracts them into the dummy folder.
 * The extracted models can be parsed like any other process models from the BPM AI.
 * @param rootDir container of archives to extract
 * @param dummyFolder folder to extract the models to
 * @throws ZipException if archive extraction went wrong
 * @throws IOException if one of the given paths can not be read or written
 *///from w  w  w.j  a  va  2 s. c  om
private void extractAvailableSgxArchives(File rootDir, File dummyFolder) throws ZipException, IOException {
    for (File file : rootDir.listFiles()) {
        if ((!file.isDirectory()) && (file.getName().endsWith(".sgx"))) {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            //iterate through files of an zip archive
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String entryName = entry.getName();
                if (entryName.contains("/")) {
                    //ignore meta data files
                    if (entryName.endsWith("_meta.json")) {
                        continue;
                    }
                    //remove directory folder to fit into expected structure
                    String[] pathParts = entryName.split("/");
                    if (entryName.contains("directory_")) {
                        entryName = "";
                        for (int i = 0; i < pathParts.length; i++) {
                            if (!(pathParts[i].startsWith("directory_"))) {
                                entryName = entryName.concat(pathParts[i] + "/");
                            }
                        }
                        entryName = entryName.substring(0, entryName.length() - 1);
                    }
                    //rename process model files
                    String oldModelName = pathParts[pathParts.length - 1];
                    String[] nameParts = oldModelName.split("_");
                    if (nameParts.length > 2) {
                        String modelName = pathParts[pathParts.length - 2].split("_")[1] + "_rev" + nameParts[1]
                                + nameParts[2];
                        entryName = entryName.replace(oldModelName, modelName);
                    }
                    //create directories
                    (new File(dummyFolder.getPath() + File.separatorChar
                            + entryName.substring(0, entryName.lastIndexOf("/")))).mkdirs();
                }
                //extract process model
                copyInputStream(zipFile.getInputStream(entry),
                        dummyFolder.getPath() + File.separatorChar + entryName);
            }
            zipFile.close();
        }
    }
}

From source file:com.adobe.aem.demomachine.communities.Loader.java

public static void main(String[] args) {

    String hostname = null;/*w  w  w .jav  a  2s  .  c  om*/
    String port = null;
    String altport = null;
    String csvfile = null;
    String analytics = null;
    String adminPassword = "admin";
    boolean reset = false;
    boolean configure = false;
    boolean minimize = false;
    boolean noenablement = true;
    int maxretries = MAXRETRIES;

    // Command line options for this tool
    Options options = new Options();
    options.addOption("h", true, "Hostname");
    options.addOption("p", true, "Port");
    options.addOption("a", true, "Alternate Port");
    options.addOption("f", true, "CSV file");
    options.addOption("r", false, "Reset");
    options.addOption("u", true, "Admin Password");
    options.addOption("c", false, "Configure");
    options.addOption("m", false, "Minimize");
    options.addOption("e", false, "No Enablement");
    options.addOption("s", true, "Analytics Endpoint");
    options.addOption("t", false, "Analytics");
    options.addOption("w", false, "Retry");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            hostname = cmd.getOptionValue("h");
        }

        if (cmd.hasOption("p")) {
            port = cmd.getOptionValue("p");
        }

        if (cmd.hasOption("a")) {
            altport = cmd.getOptionValue("a");
        }

        if (cmd.hasOption("f")) {
            csvfile = cmd.getOptionValue("f");
        }

        if (cmd.hasOption("u")) {
            adminPassword = cmd.getOptionValue("u");
        }

        if (cmd.hasOption("t")) {
            if (cmd.hasOption("s")) {
                analytics = cmd.getOptionValue("s");
            }
        }

        if (cmd.hasOption("r")) {
            reset = true;
        }

        if (cmd.hasOption("w")) {
            maxretries = Integer.parseInt(cmd.getOptionValue("w"));
        }

        if (cmd.hasOption("c")) {
            configure = true;
        }

        if (cmd.hasOption("m")) {
            minimize = true;
        }

        if (cmd.hasOption("e")) {
            noenablement = false;
        }

        if (csvfile == null || port == null || hostname == null) {
            System.out.println(
                    "Request parameters: -h hostname -p port -a alternateport -u adminPassword -f path_to_CSV_file -r (true|false, delete content before import) -c (true|false, post additional properties)");
            System.exit(-1);
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    logger.debug("AEM Demo Loader: Processing file " + csvfile);

    try {

        // Reading and processing the CSV file, stand alone or as part of a ZIP file
        if (csvfile != null && csvfile.toLowerCase().endsWith(".zip")) {

            ZipFile zipFile = new ZipFile(csvfile);
            ZipInputStream stream = new ZipInputStream(new FileInputStream(csvfile));
            ZipEntry zipEntry;
            while ((zipEntry = stream.getNextEntry()) != null) {
                if (!zipEntry.isDirectory() && zipEntry.getName().toLowerCase().endsWith(".csv")) {

                    InputStream is = zipFile.getInputStream(zipEntry);
                    BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    processLoading(null, in, hostname, port, altport, adminPassword, analytics, reset,
                            configure, minimize, noenablement, csvfile, maxretries);

                }
            }

            try {
                stream.close();
                zipFile.close();
            } catch (IOException ioex) {
                //omitted.
            }

        } else if (csvfile.toLowerCase().endsWith(".csv")) {

            Reader in = new FileReader(csvfile);
            processLoading(null, in, hostname, port, altport, adminPassword, analytics, reset, configure,
                    minimize, noenablement, csvfile, maxretries);

        }

    } catch (IOException e) {

        logger.error(e.getMessage());

    }

}

From source file:org.openmrs.module.clinicalsummary.web.controller.upload.UploadSummariesController.java

public void validate(final String filename, final String password) throws Exception {
    String encryptedFilename = StringUtils.join(Arrays.asList(filename, TaskConstants.FILE_TYPE_ENCRYPTED),
            ".");
    ZipFile encryptedFile = new ZipFile(new File(TaskUtils.getEncryptedOutputPath(), encryptedFilename));

    byte[] initVector = null;
    byte[] encryptedSampleBytes = null;
    Enumeration<? extends ZipEntry> entries = encryptedFile.entries();
    while (entries.hasMoreElements()) {
        ZipEntry zipEntry = entries.nextElement();
        String zipEntryName = zipEntry.getName();
        if (zipEntryName.endsWith(TaskConstants.FILE_TYPE_SECRET)) {
            InputStream inputStream = encryptedFile.getInputStream(zipEntry);
            initVector = FileCopyUtils.copyToByteArray(inputStream);
            if (initVector.length != IV_SIZE) {
                throw new Exception("Secret file is corrupted or invalid secret file are being used.");
            }//from   ww w  . j  a  v  a 2s . com
        } else if (zipEntryName.endsWith(TaskConstants.FILE_TYPE_SAMPLE)) {
            InputStream inputStream = encryptedFile.getInputStream(zipEntry);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            FileCopyUtils.copy(inputStream, baos);
            encryptedSampleBytes = baos.toByteArray();
        }
    }

    if (initVector != null && encryptedSampleBytes != null) {
        SecretKeyFactory factory = SecretKeyFactory.getInstance(TaskConstants.SECRET_KEY_FACTORY);
        KeySpec spec = new PBEKeySpec(password.toCharArray(), password.getBytes(), 1024, 128);
        SecretKey tmp = factory.generateSecret(spec);
        // generate the secret key
        SecretKey secretKey = new SecretKeySpec(tmp.getEncoded(), TaskConstants.KEY_SPEC);
        // create the cipher
        Cipher cipher = Cipher.getInstance(TaskConstants.CIPHER_CONFIGURATION);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(initVector));
        // decrypt the sample
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(encryptedSampleBytes);
        CipherInputStream cipherInputStream = new CipherInputStream(byteArrayInputStream, cipher);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FileCopyUtils.copy(cipherInputStream, baos);

        String sampleText = baos.toString();
        if (!sampleText.contains("This is sample text")) {
            throw new Exception("Upload parameters incorrect!");
        }
    }
}