List of usage examples for java.util.zip ZipFile getEntry
public ZipEntry getEntry(String name)
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 . j av a 2s . com*/ //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.commonjava.maven.galley.filearc.internal.ZipPublish.java
private Boolean rewriteArchive(final File dest, final String path) { final boolean isJar = isJarOperation(); final File src = new File(dest.getParentFile(), dest.getName() + ".old"); dest.renameTo(src);// w w w .j a v a 2 s.c o m InputStream zin = null; ZipFile zfIn = null; FileOutputStream fos = null; ZipOutputStream zos = null; ZipFile zfOut = null; try { fos = new FileOutputStream(dest); zos = isJar ? new JarOutputStream(fos) : new ZipOutputStream(fos); zfOut = isJar ? new JarFile(dest) : new ZipFile(dest); zfIn = isJar ? new JarFile(src) : new ZipFile(src); for (final Enumeration<? extends ZipEntry> en = zfIn.entries(); en.hasMoreElements();) { final ZipEntry inEntry = en.nextElement(); final String inPath = inEntry.getName(); try { if (inPath.equals(path)) { zin = stream; } else { zin = zfIn.getInputStream(inEntry); } final ZipEntry entry = zfOut.getEntry(inPath); zos.putNextEntry(entry); copy(stream, zos); } finally { closeQuietly(zin); } } return true; } catch (final IOException e) { error = new TransferException("Failed to write path: %s to EXISTING archive: %s. Reason: %s", e, path, dest, e.getMessage()); } finally { closeQuietly(zos); closeQuietly(fos); if (zfOut != null) { //noinspection EmptyCatchBlock try { zfOut.close(); } catch (final IOException e) { } } if (zfIn != null) { //noinspection EmptyCatchBlock try { zfIn.close(); } catch (final IOException e) { } } closeQuietly(stream); } return false; }
From source file:org.digidoc4j.impl.BDocContainer.java
private String getBdocMimeTypeFromZip(String path) { logger.debug("Get mime type from zip for " + path); String mimeType;//w w w . j a v a2 s. co m try { ZipFile zipFile = new ZipFile(path); ZipEntry entry = zipFile.getEntry("mimetype"); if (entry == null) { logger.error("Unsupported format, mimetype missing"); throw new UnsupportedFormatException("Not an asic-e document. Mimetype is missing."); } InputStream stream = zipFile.getInputStream(entry); mimeType = IOUtils.toString(stream); stream.close(); zipFile.close(); } catch (IOException e) { e.printStackTrace(); throw new DigiDoc4JException(e); } logger.debug("Mime type " + mimeType); return mimeType; }
From source file:com.meltmedia.cadmium.core.util.WarUtils.java
/** * <p>This method updates a template war with the following settings.</p> * @param templateWar The name of the template war to pull from the classpath. * @param war The path of an external war to update (optional). * @param newWarNames The name to give the new war. (note: only the first element of this list is used.) * @param repoUri The uri to a github repo to pull content from. * @param branch The branch of the github repo to pull content from. * @param configRepoUri The uri to a github repo to pull config from. * @param configBranch The branch of the github repo to pull config from. * @param domain The domain to bind a vHost to. * @param context The context root that this war will deploy to. * @param secure A flag to set if this war needs to have its contents password protected. * @throws Exception// w w w. j a v a2 s .c om */ public static void updateWar(String templateWar, String war, List<String> newWarNames, String repoUri, String branch, String configRepoUri, String configBranch, String domain, String context, boolean secure, Logger log) throws Exception { ZipFile inZip = null; ZipOutputStream outZip = null; InputStream in = null; OutputStream out = null; try { if (war != null) { if (war.equals(newWarNames.get(0))) { File tmpZip = File.createTempFile(war, null); tmpZip.delete(); tmpZip.deleteOnExit(); new File(war).renameTo(tmpZip); war = tmpZip.getAbsolutePath(); } inZip = new ZipFile(war); } else { File tmpZip = File.createTempFile("cadmium-war", "war"); tmpZip.delete(); tmpZip.deleteOnExit(); in = WarUtils.class.getClassLoader().getResourceAsStream(templateWar); out = new FileOutputStream(tmpZip); FileSystemManager.streamCopy(in, out); inZip = new ZipFile(tmpZip); } outZip = new ZipOutputStream(new FileOutputStream(newWarNames.get(0))); ZipEntry cadmiumPropertiesEntry = null; cadmiumPropertiesEntry = inZip.getEntry("WEB-INF/cadmium.properties"); Properties cadmiumProps = updateProperties(inZip, cadmiumPropertiesEntry, repoUri, branch, configRepoUri, configBranch); ZipEntry jbossWeb = null; jbossWeb = inZip.getEntry("WEB-INF/jboss-web.xml"); Enumeration<? extends ZipEntry> entries = inZip.entries(); while (entries.hasMoreElements()) { ZipEntry e = entries.nextElement(); if (e.getName().equals(cadmiumPropertiesEntry.getName())) { storeProperties(outZip, cadmiumPropertiesEntry, cadmiumProps, newWarNames); } else if (((domain != null && domain.length() > 0) || (context != null && context.length() > 0)) && e.getName().equals(jbossWeb.getName())) { updateDomain(inZip, outZip, jbossWeb, domain, context); } else if (secure && e.getName().equals("WEB-INF/web.xml")) { addSecurity(inZip, outZip, e); } else { outZip.putNextEntry(e); if (!e.isDirectory()) { FileSystemManager.streamCopy(inZip.getInputStream(e), outZip, true); } outZip.closeEntry(); } } } finally { if (FileSystemManager.exists("tmp_cadmium-war.war")) { new File("tmp_cadmium-war.war").delete(); } try { if (inZip != null) { inZip.close(); } } catch (Exception e) { if (log != null) { log.error("Failed to close " + war); } } try { if (outZip != null) { outZip.close(); } } catch (Exception e) { if (log != null) { log.error("Failed to close " + newWarNames.get(0)); } } try { if (out != null) { out.close(); } } catch (Exception e) { } try { if (in != null) { in.close(); } } catch (Exception e) { } } }
From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java
@Test public void doubleSave() throws Exception { UCFPackage container = new UCFPackage(); container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE); container.addResource("Hello there ", "helloworld.txt", "text/plain"); container.save(tmpFile);//ww w.j av a2s . co m container.addResource("Hello again", "again.txt", "text/plain"); container.save(tmpFile); ZipFile zipFile = new ZipFile(tmpFile); assertNotNull(zipFile.getEntry("helloworld.txt")); assertNotNull(zipFile.getEntry("again.txt")); }
From source file:com.hichinaschool.flashcards.libanki.Utils.java
public static boolean unzipFiles(ZipFile zipFile, String targetDirectory, String[] zipEntries, HashMap<String, String> zipEntryToFilenameMap) { byte[] buf = new byte[FILE_COPY_BUFFER_SIZE]; File dir = new File(targetDirectory); if (!dir.exists() && !dir.mkdirs()) { Log.e(AnkiDroidApp.TAG, "Utils.unzipFiles: Could not create target directory: " + targetDirectory); return false; }// www .j a v a 2 s . c o m if (zipEntryToFilenameMap == null) { zipEntryToFilenameMap = new HashMap<String, String>(); } BufferedInputStream zis = null; BufferedOutputStream bos = null; try { for (String requestedEntry : zipEntries) { ZipEntry ze = zipFile.getEntry(requestedEntry); if (ze != null) { String name = ze.getName(); if (zipEntryToFilenameMap.containsKey(name)) { name = zipEntryToFilenameMap.get(name); } File destFile = new File(dir, name); File parentDir = destFile.getParentFile(); if (!parentDir.exists() && !parentDir.mkdirs()) { return false; } if (!ze.isDirectory()) { // Log.i(AnkiDroidApp.TAG, "uncompress " + name); zis = new BufferedInputStream(zipFile.getInputStream(ze)); bos = new BufferedOutputStream(new FileOutputStream(destFile), FILE_COPY_BUFFER_SIZE); int n; while ((n = zis.read(buf, 0, FILE_COPY_BUFFER_SIZE)) != -1) { bos.write(buf, 0, n); } bos.flush(); bos.close(); zis.close(); } } } } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "Utils.unzipFiles: Error while unzipping archive.", e); return false; } finally { try { if (bos != null) { bos.close(); } } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "Utils.unzipFiles: Error while closing output stream.", e); } try { if (zis != null) { zis.close(); } } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "Utils.unzipFiles: Error while closing zip input stream.", e); } } return true; }
From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java
@Test public void doubleSaveOutputStream() throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); UCFPackage container = new UCFPackage(); container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE); container.addResource("Hello there ", "helloworld.txt", "text/plain"); container.save(outStream);//from w w w. ja va 2 s .c om container.addResource("Hello again", "again.txt", "text/plain"); outStream.close(); outStream = new ByteArrayOutputStream(); container.save(outStream); outStream.close(); FileUtils.writeByteArrayToFile(tmpFile, outStream.toByteArray()); ZipFile zipFile = new ZipFile(tmpFile); assertNotNull(zipFile.getEntry("helloworld.txt")); assertNotNull(zipFile.getEntry("again.txt")); }
From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java
@Test public void workflowBundleMimeType() throws Exception { UCFPackage container = new UCFPackage(); container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE); assertEquals(UCFPackage.MIME_WORKFLOW_BUNDLE, container.getPackageMediaType()); container.save(tmpFile);//from w w w. j av a2s . c o m ZipFile zipFile = new ZipFile(tmpFile); ZipEntry mimeEntry = zipFile.getEntry("mimetype"); assertEquals("mimetype", mimeEntry.getName()); assertEquals("Wrong mimetype", UCFPackage.MIME_WORKFLOW_BUNDLE, IOUtils.toString(zipFile.getInputStream(mimeEntry), "ASCII")); }
From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java
@Test public void manifestMimetype() throws Exception { UCFPackage container = new UCFPackage(); container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE); container.save(tmpFile);/*from www . j a v a2s .c om*/ ZipFile zipFile = new ZipFile(tmpFile); ZipEntry manifestEntry = zipFile.getEntry("META-INF/manifest.xml"); InputStream manifestStream = zipFile.getInputStream(manifestEntry); //System.out.println(IOUtils.toString(manifestStream, "UTF-8")); /* <?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"> </manifest:manifest> */ Document doc = parseXml(manifestStream); assertEquals(MANIFEST_NS, doc.getRootElement().getNamespace()); assertEquals("manifest", doc.getRootElement().getNamespacePrefix()); assertEquals("manifest:manifest", doc.getRootElement().getQualifiedName()); assertNull(xpathSelectElement(doc.getRootElement(), "/manifest:manifest/*")); }
From source file:org.apache.taverna.scufl2.ucfpackage.TestUCFPackage.java
@Test public void fileEntryFromBytes() throws Exception { UCFPackage container = new UCFPackage(); container.setPackageMediaType(UCFPackage.MIME_WORKFLOW_BUNDLE); byte[] bytes = makeBytes(1024); container.addResource(bytes, "binary", UCFPackage.MIME_BINARY); container.save(tmpFile);//from w ww . j a va 2s . c o m ZipFile zipFile = new ZipFile(tmpFile); ZipEntry manifestEntry = zipFile.getEntry("META-INF/manifest.xml"); InputStream manifestStream = zipFile.getInputStream(manifestEntry); //System.out.println(IOUtils.toString(manifestStream, "UTF-8")); /* <?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"> <manifest:file-entry manifest:media-type="application/octet-stream" manifest:full-path="binary" manifest:size="1024"/> </manifest:manifest> */ Document doc = parseXml(manifestStream); assertEquals(MANIFEST_NS, doc.getRootElement().getNamespace()); assertEquals("manifest", doc.getRootElement().getNamespacePrefix()); assertEquals("manifest:manifest", doc.getRootElement().getQualifiedName()); assertXpathEquals("application/octet-stream", doc.getRootElement(), "/manifest:manifest/manifest:file-entry/@manifest:media-type"); assertXpathEquals("binary", doc.getRootElement(), "/manifest:manifest/manifest:file-entry/@manifest:full-path"); assertXpathEquals("1024", doc.getRootElement(), "/manifest:manifest/manifest:file-entry/@manifest:size"); InputStream io = zipFile.getInputStream(zipFile.getEntry("binary")); assertArrayEquals(bytes, IOUtils.toByteArray(io)); }