List of usage examples for java.util.zip ZipFile getInputStream
public InputStream getInputStream(ZipEntry entry) throws IOException
From source file:br.univali.celine.lms.utils.zip.Zip.java
public void unzip(File zipFile, File dir) throws Exception { InputStream is = null;/*from ww w. ja v a 2 s . com*/ OutputStream os = null; ZipFile zip = null; try { zip = new ZipFile(zipFile); Enumeration<? extends ZipEntry> e = zip.entries(); byte[] buffer = new byte[2048]; int currentEntry = 0; while (e.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) e.nextElement(); File file = new File(dir, zipEntry.getName()); currentEntry++; if (zipEntry.isDirectory()) { if (!file.exists()) file.mkdirs(); continue; } if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); try { int bytesLidos = 0; is = zip.getInputStream(zipEntry); os = new FileOutputStream(file); if (is == null) throw new ZipException("Erro ao ler a entrada do zip: " + zipEntry.getName()); while ((bytesLidos = is.read(buffer)) > 0) os.write(buffer, 0, bytesLidos); if (listener != null) listener.unzip((100 * currentEntry) / zip.size()); } finally { if (is != null) try { is.close(); } catch (Exception ex) { } if (os != null) try { os.close(); } catch (Exception ex) { } } } } finally { if (zip != null) try { zip.close(); } catch (Exception e) { } } }
From source file:de.tudarmstadt.ukp.clarin.webanno.project.page.ImportUtil.java
/** * Copy curation documents from the exported project * @param zip the ZIP file./*from w w w. j av a 2 s . c o m*/ * @param aProject the project. * @param aRepository the repository service. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("rawtypes") public static void createCurationDocumentContent(ZipFile zip, Project aProject, RepositoryService aRepository) throws IOException { for (Enumeration zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements();) { ZipEntry entry = (ZipEntry) zipEnumerate.nextElement(); // Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985) String entryName = normalizeEntryName(entry); if (entryName.startsWith(CURATION_AS_SERIALISED_CAS)) { String fileName = entryName.replace(CURATION_AS_SERIALISED_CAS, ""); // the user annotated the document is file name minus extension // (anno1.ser) String username = FilenameUtils.getBaseName(fileName).replace(".ser", ""); // name of the annotation document fileName = fileName.replace(FilenameUtils.getName(fileName), "").replace("/", ""); de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument = aRepository .getSourceDocument(aProject, fileName); File annotationFilePath = aRepository.getCasFile(sourceDocument, username); FileUtils.copyInputStreamToFile(zip.getInputStream(entry), annotationFilePath); LOG.info("Imported curation document content for user [" + username + "] for source document [" + sourceDocument.getId() + "] in project [" + aProject.getName() + "] with id [" + aProject.getId() + "]"); } } }
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 ww w . j a v a2 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.broad.igv.feature.genome.GenomeManager.java
/** * Rewrite the {@link Globals#GENOME_ARCHIVE_SEQUENCE_FILE_LOCATION_KEY} property to equal * the specified {@code newSequencePath}. Works by creating a temp file and renaming * * @param targetFile A .genome file, in zip format * @param newSequencePath/*from www . java 2s. c o m*/ * @return boolean indicating success or failure. * @throws IOException */ static boolean rewriteSequenceLocation(File targetFile, String newSequencePath) throws IOException { ZipFile targetZipFile = new ZipFile(targetFile); boolean success = false; File tmpZipFile = File.createTempFile("tmpGenome", ".zip"); ZipEntry propEntry = targetZipFile.getEntry(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME); InputStream propertyInputStream = null; ZipOutputStream zipOutputStream = null; Properties inputProperties = new Properties(); try { propertyInputStream = targetZipFile.getInputStream(propEntry); BufferedReader reader = new BufferedReader(new InputStreamReader(propertyInputStream)); //Copy over property.txt, only replacing a few properties inputProperties.load(reader); inputProperties.put(Globals.GENOME_ARCHIVE_SEQUENCE_FILE_LOCATION_KEY, newSequencePath); inputProperties.put(Globals.GENOME_ARCHIVE_CUSTOM_SEQUENCE_LOCATION_KEY, Boolean.TRUE.toString()); ByteArrayOutputStream propertyBytes = new ByteArrayOutputStream(); PrintWriter propertyFileWriter = new PrintWriter(new OutputStreamWriter(propertyBytes)); inputProperties.store(propertyFileWriter, null); propertyFileWriter.flush(); byte[] newPropertyBytes = propertyBytes.toByteArray(); Enumeration<? extends ZipEntry> entries = targetZipFile.entries(); zipOutputStream = new ZipOutputStream(new FileOutputStream(tmpZipFile)); while (entries.hasMoreElements()) { ZipEntry curEntry = entries.nextElement(); ZipEntry writeEntry = null; if (curEntry.getName().equals(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME)) { writeEntry = new ZipEntry(Globals.GENOME_ARCHIVE_PROPERTY_FILE_NAME); writeEntry.setSize(newPropertyBytes.length); zipOutputStream.putNextEntry(writeEntry); zipOutputStream.write(newPropertyBytes); continue; } else { //Because the compressed size can vary, //we generate a new ZipEntry and copy some attributes writeEntry = new ZipEntry(curEntry.getName()); writeEntry.setSize(curEntry.getSize()); writeEntry.setComment(curEntry.getComment()); writeEntry.setTime(curEntry.getTime()); } zipOutputStream.putNextEntry(writeEntry); InputStream tmpIS = null; try { tmpIS = targetZipFile.getInputStream(writeEntry); int bytes = IOUtils.copy(tmpIS, zipOutputStream); log.debug(bytes + " bytes written to " + targetFile); } finally { if (tmpIS != null) tmpIS.close(); } } } catch (Exception e) { tmpZipFile.delete(); throw new RuntimeException(e.getMessage(), e); } finally { if (propertyInputStream != null) propertyInputStream.close(); if (zipOutputStream != null) { zipOutputStream.flush(); zipOutputStream.finish(); zipOutputStream.close(); } zipOutputStream = null; System.gc(); success = true; } //This is a hack. I don't know why it's necessary, //but for some reason the output zip file seems to be corrupt //at least when called from GenomeManager.refreshArchive try { Thread.sleep(1500); } catch (InterruptedException e) { // } //Rename tmp file if (success) { targetFile.delete(); FileUtils.copyFile(tmpZipFile, targetFile); success = targetFile.exists(); tmpZipFile.delete(); } return success; }
From source file:de.tudarmstadt.ukp.clarin.webanno.project.page.ImportUtil.java
/** * copy annotation documents (serialized CASs) from the exported project * @param zip the ZIP file.//ww w. ja va2 s . com * @param aProject the project. * @param aRepository the repository service. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("rawtypes") public static void createAnnotationDocumentContent(ZipFile zip, Project aProject, RepositoryService aRepository) throws IOException { for (Enumeration zipEnumerate = zip.entries(); zipEnumerate.hasMoreElements();) { ZipEntry entry = (ZipEntry) zipEnumerate.nextElement(); // Strip leading "/" that we had in ZIP files prior to 2.0.8 (bug #985) String entryName = normalizeEntryName(entry); if (entryName.startsWith(ANNOTATION_AS_SERIALISED_CAS + "/")) { String fileName = entryName.replace(ANNOTATION_AS_SERIALISED_CAS + "/", ""); // the user annotated the document is file name minus extension // (anno1.ser) String username = FilenameUtils.getBaseName(fileName).replace(".ser", ""); // name of the annotation document fileName = fileName.replace(FilenameUtils.getName(fileName), "").replace("/", ""); de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument sourceDocument = aRepository .getSourceDocument(aProject, fileName); File annotationFilePath = aRepository.getCasFile(sourceDocument, username); FileUtils.copyInputStreamToFile(zip.getInputStream(entry), annotationFilePath); LOG.info("Imported annotation document content for user [" + username + "] for source document [" + sourceDocument.getId() + "] in project [" + aProject.getName() + "] with id [" + aProject.getId() + "]"); } } }
From source file:maltcms.ui.nb.pipelineRunner.options.LocalMaltcmsExecutionPanel.java
private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); return;//from w w w .j a v a2 s. c om } File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile))) { IOUtils.copy(inputStream, outputStream); } finally { inputStream.close(); } }
From source file:net.sf.mavenjython.JythonMojo.java
private Collection<File> extractAllFiles(File outputDirectory, ZipFile ja, Enumeration<JarEntry> en) throws MojoExecutionException { List<File> files = new ArrayList<File>(); while (en.hasMoreElements()) { JarEntry el = en.nextElement(); // getLog().info(" > " + el); if (!el.isDirectory()) { File destFile = new File(outputDirectory, el.getName()); // destFile = new File(outputDirectory, destFile.getName()); if (OVERRIDE || !destFile.exists()) { destFile.getParentFile().mkdirs(); try { FileOutputStream fo = new FileOutputStream(destFile); IOUtils.copy(ja.getInputStream(el), fo); fo.close();// w w w. j a va2 s. c o m } catch (IOException e) { throw new MojoExecutionException( "extracting " + el.getName() + " from jython artifact jar failed", e); } } files.add(destFile); } } return files; }
From source file:org.liberty.android.fantastischmemopro.downloader.DownloaderAnyMemo.java
private void downloadDatabase(final DownloadItem di) throws Exception { String filename = di.getExtras("filename"); if (filename == null) { throw new Exception("Could not get filename"); }//from w w w . ja va 2 s. c o m String sdpath = Environment.getExternalStorageDirectory().getAbsolutePath() + getString(R.string.default_dir); File outFile = new File(sdpath + filename); mHandler.post(new Runnable() { public void run() { mProgressDialog = new ProgressDialog(DownloaderAnyMemo.this); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setMessage(getString(R.string.loading_downloading)); mProgressDialog.show(); } }); try { OutputStream out; if (outFile.exists()) { throw new IOException("Database already exist!"); } try { outFile.createNewFile(); out = new FileOutputStream(outFile); URL myURL = new URL(di.getAddress()); Log.v(TAG, "URL IS: " + myURL); URLConnection ucon = myURL.openConnection(); final int fileSize = ucon.getContentLength(); mHandler.post(new Runnable() { public void run() { mProgressDialog.setMax(fileSize); } }); byte[] buf = new byte[8192]; InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, 8192); Runnable increaseProgress = new Runnable() { public void run() { mProgressDialog.setProgress(mDownloadProgress); } }; int len = 0; int lenSum = 0; while ((len = bis.read(buf)) != -1) { out.write(buf, 0, len); lenSum += len; if (lenSum > fileSize / 50) { /* This is tricky. * The UI thread can not be updated too often. * So we update it only 50 times */ mDownloadProgress += lenSum; lenSum = 0; mHandler.post(increaseProgress); } } out.close(); is.close(); /* Uncompress the zip file that contains images */ if (filename.endsWith(".zip")) { mHandler.post(new Runnable() { public void run() { mProgressDialog.setProgress(fileSize); mProgressDialog.setMessage(getString(R.string.downloader_extract_zip)); } }); BufferedOutputStream dest = null; BufferedInputStream ins = null; ZipEntry entry; ZipFile zipfile = new ZipFile(outFile); Enumeration<?> e = zipfile.entries(); while (e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); Log.v(TAG, "Extracting: " + entry); if (entry.isDirectory()) { new File(sdpath + "/" + entry.getName()).mkdir(); } else { ins = new BufferedInputStream(zipfile.getInputStream(entry), 8192); int count; byte data[] = new byte[8192]; FileOutputStream fos = new FileOutputStream(sdpath + "/" + entry.getName()); dest = new BufferedOutputStream(fos, 8192); while ((count = ins.read(data, 0, 8192)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); ins.close(); } } /* Delete the zip file if it is successfully decompressed */ outFile.delete(); } /* We do not check ttf file as db */ if (!filename.toLowerCase().endsWith(".ttf")) { /* Check if the db is correct */ filename = filename.replace(".zip", ".db"); DatabaseHelper dh = new DatabaseHelper(DownloaderAnyMemo.this, sdpath, filename); dh.close(); } } catch (Exception e) { if (outFile.exists()) { outFile.delete(); } throw new Exception(e); } } catch (Exception e) { Log.e(TAG, "Error downloading", e); throw new Exception(e); } finally { mHandler.post(new Runnable() { public void run() { mProgressDialog.dismiss(); } }); } }
From source file:org.ambraproject.article.service.XslIngestArchiveProcessor.java
/** * Helper method to extract XML from the zip file * * @param zipFile - the zip file containing the file to extract * @param fileName - the file to extract * @return - the parsed xml file//from ww w . ja v a2s.c o m * @throws java.io.IOException - if there's a problem reading from the zip file * @throws org.xml.sax.SAXException - if there's a problem parsing the xml */ private Document extractXml(ZipFile zipFile, String fileName) throws IOException, SAXException { InputStream inputStream = null; try { inputStream = zipFile.getInputStream(zipFile.getEntry(fileName)); return documentBuilder.parse(inputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { log.warn("Error closing zip input stream during ingest processing", e); } } } }
From source file:org.esa.s1tbx.sentinel1.gpf.EAPPhaseCorrectionOp.java
private void readAuxCalFile() throws Exception { try {//from w w w . j ava 2 s . com final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); final DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); final Document doc; if (auxCalFile.getName().toLowerCase().endsWith(".zip")) { final ZipFile productZip = new ZipFile(auxCalFile, ZipFile.OPEN_READ); final Enumeration<? extends ZipEntry> entries = productZip.entries(); final ZipEntry folderEntry = entries.nextElement(); final ZipEntry zipEntry = productZip.getEntry(folderEntry.getName() + "data/s1a-aux-cal.xml"); InputStream is = productZip.getInputStream(zipEntry); doc = documentBuilder.parse(is); } else { doc = documentBuilder.parse(auxCalFile); } if (doc == null) { System.out.println("EAPPhaseCorrection: failed to create Document for AUX_CAL file"); return; } doc.getDocumentElement().normalize(); final org.w3c.dom.Node calibrationParamsListNode = doc.getElementsByTagName("auxiliaryCalibration") .item(0).getChildNodes().item(1); org.w3c.dom.Node childNode = calibrationParamsListNode.getFirstChild(); while (childNode != null) { if (childNode.getNodeName().equals("calibrationParams")) { final String swath = getNodeTextContent(childNode, "swath"); if (swath != null && swath.contains(acquisitionMode)) { final String pol = getNodeTextContent(childNode, "polarisation"); readOneEAPVector(childNode, swath, pol.toUpperCase()); } } childNode = childNode.getNextSibling(); } } catch (IOException e) { System.out.println("EAPPhaseCorrection: IOException " + e.getMessage()); } catch (ParserConfigurationException e) { System.out.println("EAPPhaseCorrection: ParserConfigurationException " + e.getMessage()); } catch (SAXException e) { System.out.println("EAPPhaseCorrection: SAXException " + e.getMessage()); } catch (Exception e) { System.out.println("EAPPhaseCorrection: Exception " + e.getMessage()); } }