List of usage examples for java.util.zip ZipFile close
public void close() throws IOException
From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java
private HashMap<String, String> parseEpub(File file) throws IOException, TMFVisibleException { LOG.debug("[parseEpub] - BEGIN"); ZipFile fi = new ZipFile(file); for (Enumeration e = fi.entries(); e.hasMoreElements();) { ZipEntry entry = (ZipEntry) e.nextElement(); if (entry.getName().endsWith("ncx")) { InputStream tocMaybeDirty = fi.getInputStream(entry); Scanner scanner = new Scanner(tocMaybeDirty, "UTF-8").useDelimiter("\\A"); String theString = scanner.hasNext() ? scanner.next() : ""; tocMaybeDirty.close();/*from w w w. j ava 2 s .c om*/ scanner.close(); String res = theString.replaceAll(">[\\s]*?<", "><"); InputStream toc = new ByteArrayInputStream(res.getBytes(StandardCharsets.UTF_8)); try { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = dBuilder.parse(toc); toc.close(); if (doc.hasChildNodes()) { findNavMap(doc.getChildNodes()); } } catch (Exception ex) { LOG.error("Unable to navigate the TOC"); } removeEmptyTOC(epub); //search anchors in links and split Set set = epub.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry me = (Map.Entry) i.next(); if (me.getValue().toString().contains("#")) { String[] parts = me.getValue().toString().split("#"); String anchor = parts[1]; epub.put(me.getKey().toString(), anchor); } } } if (entry.getName().endsWith("opf")) { //manage files because order is important InputStream content = fi.getInputStream(entry); Scanner scanner = new Scanner(content, "UTF-8").useDelimiter("\\A"); String contentString = scanner.hasNext() ? scanner.next() : ""; content.close(); scanner.close(); String filenameRegex = "href=\"(.*.htm(|l))\".*media-type=\"application/xhtml"; Pattern pattern = Pattern.compile(filenameRegex); Matcher matcher = pattern.matcher(contentString); Integer count = 0; while (matcher.find()) { files.put(count, matcher.group(1)); count++; } } if (entry.getName().endsWith("html") || entry.getName().endsWith("htm") || entry.getName().endsWith("xhtml")) { InputStream htmlFile = fi.getInputStream(entry); Scanner scanner = new Scanner(htmlFile, "UTF-8").useDelimiter("\\A"); String htmlString = scanner.hasNext() ? scanner.next() : ""; String regex1 = htmlString.replaceAll("^[^_]*?<body>", ""); //remove head String regex2 = regex1.replaceAll("</body>.*$", ""); //remove tail String htmlCleaned = regex2.replaceAll("<a.*?/>", ""); //anchor with one tag String[] bits = entry.getName().split("/"); String fileName = bits[bits.length - 1]; htmls.put(fileName, htmlCleaned); } } fi.close(); Integer i; for (i = 0; i < files.size(); i++) { stringBuilder.append("<p id=\"" + files.get(i) + "\"></p>"); // "anchor" also the heads of each files stringBuilder.append(htmls.get(files.get(i))); } String htmlAll = stringBuilder.toString(); /* We have all needed files, start to split For each link -> made a chunk Start from the bottom */ Metadata metadata = new Metadata(); Parser parser = new HtmlParser(); ListIterator<Map.Entry<String, String>> iter = new ArrayList<>(epub.entrySet()).listIterator(epub.size()); while (iter.hasPrevious()) { Map.Entry<String, String> me = iter.previous(); try { ContentHandler contenthandler = new BodyContentHandler(10 * htmlAll.length()); Scanner sc = new Scanner(htmlAll); sc.useDelimiter("id=\"" + me.getValue().toString() + "\">"); htmlAll = sc.next(); InputStream stream = new ByteArrayInputStream(sc.next().getBytes(StandardCharsets.UTF_8)); parser.parse(stream, contenthandler, metadata, new ParseContext()); String chapterText = contenthandler.toString().toLowerCase().replaceAll("\\d+.*", ""); String chapterTextWithoutNo = chapterText.replaceAll("\\d+.*", ""); // Remove the Project Gutenberg meta information from the text String chapterTextCleaned = chapterTextWithoutNo.split("end of the project gutenberg ebook")[0]; epub.put(me.getKey().toString(), chapterTextCleaned); } catch (Exception ex) { LOG.error("Unable to parse content for index: " + me.getKey() + ", this chapter will be deleted"); removeChapter(epub, me.getKey().toString()); } } /* I remove the Project Gutenberg license chapter from the Map, because it is useless for the classification and it generates a Lucene Exception in case of the Italian language (the license text is always in English). You can use this method in order to remove each chapter that is useless for classifying your Epub document. */ removeChapter(epub, "A Word from Project Gutenberg"); removeEmptyItems(epub); //If the Epub file has a bad structure, I try to use the basic Epub extractor of Tika. if (epub.size() == 0) { LOG.info("The Epub file has a bad structure. Try to use the Tika extractor"); epub.put("All text", autoParseAll(file)); } removeEmptyItems(epub); if (epub.size() == 0) { LOG.error("Unable to extract text from this Epub"); throw new TMFVisibleException("Unable to extract any text from this Epub."); } removeDownloadedFile(TEMPORARY_PATH); LOG.debug("[parseEpub] - END"); return epub; }
From source file:org.dspace.app.itemimport.ItemImportServiceImpl.java
@Override public String unzip(File zipfile, String destDir) throws IOException { // 2/*from w w w.j av a 2s . c o m*/ // does the zip file exist and can we write to the temp directory if (!zipfile.canRead()) { log.error("Zip file '" + zipfile.getAbsolutePath() + "' does not exist, or is not readable."); } String destinationDir = destDir; if (destinationDir == null) { destinationDir = tempWorkDir; } File tempdir = new File(destinationDir); if (!tempdir.isDirectory()) { log.error("'" + ConfigurationManager.getProperty("org.dspace.app.itemexport.work.dir") + "' as defined by the key 'org.dspace.app.itemexport.work.dir' in dspace.cfg " + "is not a valid directory"); } if (!tempdir.exists() && !tempdir.mkdirs()) { log.error("Unable to create temporary directory: " + tempdir.getAbsolutePath()); } String sourcedir = destinationDir + System.getProperty("file.separator") + zipfile.getName(); String zipDir = destinationDir + System.getProperty("file.separator") + zipfile.getName() + System.getProperty("file.separator"); // 3 String sourceDirForZip = sourcedir; ZipFile zf = new ZipFile(zipfile); ZipEntry entry; Enumeration<? extends ZipEntry> entries = zf.entries(); while (entries.hasMoreElements()) { entry = entries.nextElement(); if (entry.isDirectory()) { if (!new File(zipDir + entry.getName()).mkdirs()) { log.error("Unable to create contents directory: " + zipDir + entry.getName()); } } else { System.out.println("Extracting file: " + entry.getName()); log.info("Extracting file: " + entry.getName()); int index = entry.getName().lastIndexOf('/'); if (index == -1) { // Was it created on Windows instead? index = entry.getName().lastIndexOf('\\'); } if (index > 0) { File dir = new File(zipDir + entry.getName().substring(0, index)); if (!dir.exists() && !dir.mkdirs()) { log.error("Unable to create directory: " + dir.getAbsolutePath()); } //Entries could have too many directories, and we need to adjust the sourcedir // file1.zip (SimpleArchiveFormat / item1 / contents|dublin_core|... // SimpleArchiveFormat / item2 / contents|dublin_core|... // or // file2.zip (item1 / contents|dublin_core|... // item2 / contents|dublin_core|... //regex supports either windows or *nix file paths String[] entryChunks = entry.getName().split("/|\\\\"); if (entryChunks.length > 2) { if (StringUtils.equals(sourceDirForZip, sourcedir)) { sourceDirForZip = sourcedir + "/" + entryChunks[0]; } } } byte[] buffer = new byte[1024]; int len; InputStream in = zf.getInputStream(entry); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(zipDir + entry.getName())); while ((len = in.read(buffer)) >= 0) { out.write(buffer, 0, len); } in.close(); out.close(); } } //Close zip file zf.close(); if (!StringUtils.equals(sourceDirForZip, sourcedir)) { sourcedir = sourceDirForZip; System.out.println("Set sourceDir using path inside of Zip: " + sourcedir); log.info("Set sourceDir using path inside of Zip: " + sourcedir); } return sourcedir; }
From source file:it.readbeyond.minstrel.unzipper.Unzipper.java
private String unzip(String inputZip, String destinationDirectory, String mode, JSONObject parameters) throws IOException, JSONException { // store the zip entries to decompress List<String> list = new ArrayList<String>(); // store the zip entries actually decompressed List<String> decompressed = new ArrayList<String>(); // open input zip file File sourceZipFile = new File(inputZip); ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); // open destination directory, creating it if needed File unzipDestinationDirectory = new File(destinationDirectory); unzipDestinationDirectory.mkdirs();//w ww. j av a2s. c o m // extract all files if (mode.equals(ARGUMENT_MODE_ALL)) { Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { list.add(zipFileEntries.nextElement().getName()); } } // extract all files except audio and video // (determined by file extension) if (mode.equals(ARGUMENT_MODE_ALL_NON_MEDIA)) { String[] excludeExtensions = JSONArrayToStringArray( parameters.optJSONArray(ARGUMENT_ARGS_EXCLUDE_EXTENSIONS)); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { String name = zipFileEntries.nextElement().getName(); String lower = name.toLowerCase(); if (!isFile(lower, excludeExtensions)) { list.add(name); } } } // extract all small files // maximum size is passed in args parameter // or, if not passed, defaults to const DEFAULT_MAXIMUM_SIZE_FILE if (mode.equals(ARGUMENT_MODE_ALL_SMALL)) { long maximum_size = parameters.optLong(ARGUMENT_ARGS_MAXIMUM_FILE_SIZE, DEFAULT_MAXIMUM_SIZE_FILE); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { ZipEntry ze = zipFileEntries.nextElement(); if (ze.getSize() <= maximum_size) { list.add(ze.getName()); } } } // extract only the requested files if (mode.equals(ARGUMENT_MODE_SELECTED)) { String[] entries = JSONArrayToStringArray(parameters.optJSONArray(ARGUMENT_ARGS_ENTRIES)); for (String entry : entries) { ZipEntry ze = zipFile.getEntry(entry); if (ze != null) { list.add(entry); } } } // extract all "structural" files if (mode.equals(ARGUMENT_MODE_ALL_STRUCTURE)) { String[] extensions = JSONArrayToStringArray(parameters.optJSONArray(ARGUMENT_ARGS_EXTENSIONS)); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { String name = zipFileEntries.nextElement().getName(); String lower = name.toLowerCase(); boolean extract = isFile(lower, extensions); if (extract) { list.add(name); } } } // NOTE list contains only valid zip entries // perform unzip for (String currentEntry : list) { ZipEntry entry = zipFile.getEntry(currentEntry); File destFile = new File(unzipDestinationDirectory, currentEntry); File destinationParent = destFile.getParentFile(); destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry)); int numberOfBytesRead; byte data[] = new byte[BUFFER_SIZE]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((numberOfBytesRead = is.read(data, 0, BUFFER_SIZE)) > -1) { dest.write(data, 0, numberOfBytesRead); } dest.flush(); dest.close(); is.close(); fos.close(); decompressed.add(currentEntry); } } zipFile.close(); return stringify(decompressed); }
From source file:com.esd.ps.EmployerController.java
/** * ? zip/* w w w . ja v a 2s . c o m*/ * * @param packName * @param taskLvl */ public void storeDataZIP(String packName, int taskLvl, String url, int userId, Date date) { InputStream in = null; String zipEntryName = null; int packId = 0; try { ZipFile zip = new ZipFile(url + Constants.SLASH + packName); for (Enumeration<?> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String taskDir = Constants.EMPTY; if (entry.isDirectory()) {// ? continue; } zipEntryName = entry.getName(); if (zipEntryName.indexOf(Constants.SLASH) < zipEntryName.lastIndexOf(Constants.SLASH)) { String str[] = zipEntryName.split(Constants.SLASH); // (zipEntryName.indexOf("/") + 1) taskDir = zipEntryName.substring((zipEntryName.indexOf(Constants.SLASH) + 1), zipEntryName.lastIndexOf(Constants.SLASH)); zipEntryName = str[(str.length - 1)]; } zipEntryName = zipEntryName.substring(zipEntryName.indexOf(Constants.SLASH) + 1, zipEntryName.length()); // ? if (zipEntryName.substring((zipEntryName.length() - 3), zipEntryName.length()) .equals(Constants.WAV) == false) { // String noMatch = zipEntryName; continue; } in = zip.getInputStream(entry); // inputstrem?byte[] ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[4096]; int count = -1; while ((count = in.read(data, 0, 4096)) != -1) outStream.write(data, 0, count); data = null; byte[] wav = outStream.toByteArray(); taskWithBLOBs taskWithBLOBs = new taskWithBLOBs(); packId = packService.getPackIdByPackName(packName); // wav?0kb if (outStream.size() == 0) { taskWithBLOBs.setWorkerId(0); } taskWithBLOBs.setTaskWav(wav); taskWithBLOBs.setTaskLvl(taskLvl); // ? taskWithBLOBs.setPackId(packId); taskWithBLOBs.setTaskName(zipEntryName); // if (taskDir.trim().length() > 0) { taskDir = packName.substring(0, (packName.length() - 4)) + Constants.SLASH + taskDir; } else { taskDir = packName.substring(0, (packName.length() - 4)); } taskWithBLOBs.setTaskDir(taskDir); taskWithBLOBs.setCreateId(userId); taskWithBLOBs.setCreateTime(date); // ? taskWithBLOBs.setTaskUpload(false); StackTraceElement[] items = Thread.currentThread().getStackTrace(); taskWithBLOBs.setCreateMethod(items[1].toString()); taskWithBLOBs.setVersion(1); taskService.insert(taskWithBLOBs); } zip.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } File fd = new File(packName); fd.delete(); packWithBLOBs pack = new packWithBLOBs(); pack.setPackId(packId); pack.setUnzip(2);// ?? StackTraceElement[] items = Thread.currentThread().getStackTrace(); pack.setUpdateMethod(items[1].toString()); packService.updateByPrimaryKeySelective(pack); }
From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java
public void parse(String deathFolder) throws IOException { File f = new File(deathFolder); Runtime runtime = Runtime.getRuntime(); if (f.isDirectory()) { String[] filesInDir = f.list(); for (String fil : filesInDir) { if (fil.endsWith(".zip")) { ZipFile zipFile = new ZipFile(deathFolder + "/" + fil); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { System.out.println( "Used memory: " + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024) + " Free memory: " + (runtime.freeMemory()) / (1024 * 1024)); ZipEntry entry = entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8")); CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader() .parse(br);//ww w .ja v a 2 s . co m List<CSVRecord> records = parser.getRecords(); System.out.println("Reading records: " + zipFile.getName() + "/" + entry); /*for (int i=0;i<records.size();i++) { CSVRecord csvRecord = records.get(i);*/ for (CSVRecord csvRecord : records) { if (csvRecord.isMapped("age_group_id")) { //age group 22 corresponds to all ages if (csvRecord.get("age_group_id").equalsIgnoreCase("22")) { String location = null; String year = null; String sex = null; String cause = null; String number = null; String metric = null; if (csvRecord.isMapped("location_code")) { location = csvRecord.get("location_code"); } if (csvRecord.isMapped("year")) { year = csvRecord.get("year"); } if (csvRecord.isMapped("sex_id")) { //1=male, 2 = female if (csvRecord.get("sex_id").equalsIgnoreCase(("1"))) { sex = "m"; } else if (csvRecord.get("sex_id").equalsIgnoreCase("2")) { sex = "f"; } } if (csvRecord.isMapped("cause_name")) { cause = csvRecord.get("cause_name"); } if (csvRecord.isMapped("mean")) { number = csvRecord.get("mean"); } if (csvRecord.isMapped("metric") && csvRecord.isMapped("unit")) { metric = csvRecord.get("metric") + "-" + csvRecord.get("unit"); } if (location != null && year != null && sex != null && cause != null && number != null && metric != null) { try { sqlM.addSanitaryIssueToCountry(location, year, sex, cause, metric, number); } catch (SQLException ex) { Logger.getLogger(CSVsGBDdata.class.getName()).log(Level.SEVERE, null, ex); } } } } } parser.close(); stream.close(); br.close(); } zipFile.close(); } } } else { System.out.println("Not a directory"); } }
From source file:fll.subjective.SubjectiveFrame.java
/** * Load data. Meant for testing. Most users should use #promptForFile(). * /* w ww .ja va 2 s . c o m*/ * @param file where to read the data in from and where to save data to * @throws IOException */ public void load(final File file) throws IOException { _file = file; ZipFile zipfile = null; try { zipfile = new ZipFile(file); final ZipEntry challengeEntry = zipfile.getEntry(DownloadSubjectiveData.CHALLENGE_ENTRY_NAME); if (null == challengeEntry) { throw new FLLRuntimeException( "Unable to find challenge descriptor in file, you probably choose the wrong file or it is corrupted"); } final InputStream challengeStream = zipfile.getInputStream(challengeEntry); _challengeDocument = ChallengeParser .parse(new InputStreamReader(challengeStream, Utilities.DEFAULT_CHARSET)); challengeStream.close(); _challengeDescription = new ChallengeDescription(_challengeDocument.getDocumentElement()); final ZipEntry scoreEntry = zipfile.getEntry(DownloadSubjectiveData.SCORE_ENTRY_NAME); if (null == scoreEntry) { throw new FLLRuntimeException( "Unable to find score data in file, you probably choose the wrong file or it is corrupted"); } final InputStream scoreStream = zipfile.getInputStream(scoreEntry); _scoreDocument = XMLUtils.parseXMLDocument(scoreStream); scoreStream.close(); final ZipEntry scheduleEntry = zipfile.getEntry(DownloadSubjectiveData.SCHEDULE_ENTRY_NAME); if (null != scheduleEntry) { ObjectInputStream scheduleStream = null; try { scheduleStream = new ObjectInputStream(zipfile.getInputStream(scheduleEntry)); _schedule = (TournamentSchedule) scheduleStream.readObject(); } finally { IOUtils.closeQuietly(scheduleStream); } } else { _schedule = null; } final ZipEntry mappingEntry = zipfile.getEntry(DownloadSubjectiveData.MAPPING_ENTRY_NAME); if (null != mappingEntry) { ObjectInputStream mappingStream = null; try { mappingStream = new ObjectInputStream(zipfile.getInputStream(mappingEntry)); // ObjectStream isn't type safe @SuppressWarnings("unchecked") final Collection<CategoryColumnMapping> mappings = (Collection<CategoryColumnMapping>) mappingStream .readObject(); _scheduleColumnMappings = mappings; } finally { IOUtils.closeQuietly(mappingStream); } } else { _scheduleColumnMappings = null; } } catch (final ClassNotFoundException e) { throw new FLLInternalException("Internal error loading schedule from data file", e); } catch (final SAXParseException spe) { final String errorMessage = String.format( "Error parsing file line: %d column: %d%n Message: %s%n This may be caused by using the wrong version of the software attempting to parse a file that is not subjective data.", spe.getLineNumber(), spe.getColumnNumber(), spe.getMessage()); throw new FLLRuntimeException(errorMessage, spe); } catch (final SAXException se) { final String errorMessage = "The subjective scores file was found to be invalid, check that you are parsing a subjective scores file and not something else"; throw new FLLRuntimeException(errorMessage, se); } finally { if (null != zipfile) { try { zipfile.close(); } catch (final IOException e) { LOGGER.debug("Error closing zipfile", e); } } } for (final ScoreCategory subjectiveCategory : getChallengeDescription().getSubjectiveCategories()) { createSubjectiveTable(tabbedPane, subjectiveCategory); } // get the name and location of the tournament final Element top = _scoreDocument.getDocumentElement(); final String tournamentName = top.getAttribute("tournamentName"); if (top.hasAttribute("tournamentLocation")) { final String tournamentLocation = top.getAttribute("tournamentName"); setTitle(String.format("Subjective Score Entry - %s @ %s", tournamentName, tournamentLocation)); } else { setTitle(String.format("Subjective Score Entry - %s", tournamentName)); } pack(); }
From source file:fr.certu.chouette.gui.command.ImportCommand.java
/** * @param session/*from w w w .jav a 2 s. c o m*/ * @param save * @param savedIds * @param manager * @param importTask * @param format * @param inputFile * @param suffixes * @param values * @param importHolder * @param validationHolder * @throws ChouetteException */ private int importZipEntries(EntityManager session, boolean save, List<Long> savedIds, INeptuneManager<NeptuneIdentifiedObject> manager, ImportTask importTask, String format, String inputFile, List<String> suffixes, List<ParameterValue> values, ReportHolder importHolder, ReportHolder validationHolder) throws ChouetteException { SimpleParameterValue inputFileParam = new SimpleParameterValue("inputFile"); values.add(inputFileParam); ReportHolder zipHolder = new ReportHolder(); if (format.equalsIgnoreCase("neptune")) { SharedImportedData sharedData = new SharedImportedData(); UnsharedImportedData unsharedData = new UnsharedImportedData(); SimpleParameterValue sharedDataParam = new SimpleParameterValue("sharedImportedData"); sharedDataParam.setObjectValue(sharedData); values.add(sharedDataParam); SimpleParameterValue unsharedDataParam = new SimpleParameterValue("unsharedImportedData"); unsharedDataParam.setObjectValue(unsharedData); values.add(unsharedDataParam); } // unzip files , import and save contents ZipFile zip = null; File temp = null; File tempRep = new File(FileUtils.getTempDirectory(), "massImport" + importTask.getId()); if (!tempRep.exists()) tempRep.mkdirs(); File zipFile = new File(inputFile); ReportItem zipReportItem = new ExchangeReportItem(ExchangeReportItem.KEY.ZIP_FILE, Report.STATE.OK, zipFile.getName()); try { Charset encoding = FileTool.getZipCharset(inputFile); if (encoding == null) { ReportItem fileErrorItem = new ExchangeReportItem(ExchangeReportItem.KEY.ZIP_ERROR, Report.STATE.ERROR, "unknown encoding"); zipReportItem.addItem(fileErrorItem); importHolder.getReport().addItem(zipReportItem); saveImportReports(session, importTask, importHolder.getReport(), validationHolder.getReport()); return 1; } zip = new ZipFile(inputFile, encoding); zipHolder.setReport(zipReportItem); for (Enumeration<? extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { File dir = new File(tempRep, entry.getName()); dir.mkdirs(); continue; } if (!FilenameUtils.isExtension(entry.getName().toLowerCase(), suffixes)) { ReportItem fileReportItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_IGNORED, Report.STATE.OK, FilenameUtils.getName(entry.getName())); zipReportItem.addItem(fileReportItem); log.info("entry " + entry.getName() + " ignored, unknown extension"); continue; } InputStream stream = null; try { stream = zip.getInputStream(entry); } catch (IOException e) { ReportItem fileReportItem = new ExchangeReportItem(ExchangeReportItem.KEY.FILE_ERROR, Report.STATE.WARNING, FilenameUtils.getName(entry.getName())); zipReportItem.addItem(fileReportItem); log.error("entry " + entry.getName() + " cannot read", e); continue; } byte[] bytes = new byte[4096]; int len = stream.read(bytes); temp = new File(tempRep.getAbsolutePath() + "/" + entry.getName()); FileOutputStream fos = new FileOutputStream(temp); while (len > 0) { fos.write(bytes, 0, len); len = stream.read(bytes); } fos.close(); // import log.info("import file " + entry.getName()); inputFileParam.setFilepathValue(temp.getAbsolutePath()); List<NeptuneIdentifiedObject> beans = manager.doImport(null, format, values, zipHolder, validationHolder); if (beans != null && !beans.isEmpty()) { // save if (save) { saveBeans(manager, beans, savedIds, importHolder.getReport()); } else { for (NeptuneIdentifiedObject bean : beans) { GuiReportItem item = new GuiReportItem(GuiReportItem.KEY.NO_SAVE, Report.STATE.OK, bean.getName()); importHolder.getReport().addItem(item); } } } temp.delete(); } try { zip.close(); } catch (IOException e) { log.info("cannot close zip file"); } importHolder.getReport().addItem(zipReportItem); } catch (IOException e) { log.error("IO error", e); ReportItem fileErrorItem = new ExchangeReportItem(ExchangeReportItem.KEY.ZIP_ERROR, Report.STATE.ERROR, e.getLocalizedMessage()); zipReportItem.addItem(fileErrorItem); importHolder.getReport().addItem(zipReportItem); saveImportReports(session, importTask, importHolder.getReport(), validationHolder.getReport()); return 1; } catch (IllegalArgumentException e) { log.error("Format error", e); ReportItem fileErrorItem = new ExchangeReportItem(ExchangeReportItem.KEY.ZIP_ERROR, Report.STATE.ERROR, e.getLocalizedMessage()); zipReportItem.addItem(fileErrorItem); importHolder.getReport().addItem(zipReportItem); saveImportReports(session, importTask, importHolder.getReport(), validationHolder.getReport()); return 1; } finally { try { FileUtils.deleteDirectory(tempRep); } catch (IOException e) { log.warn("temporary directory " + tempRep.getAbsolutePath() + " could not be deleted"); } } return 0; }
From source file:org.opencms.setup.CmsSetupBean.java
/** * Reads all properties from the components.properties file at the given location, a folder or a zip file.<p> * // w ww . jav a 2 s . c o m * @param location the location to read the properties from * * @return the read properties * * @throws FileNotFoundException if the properties file could not be found * @throws CmsConfigurationException if the something else goes wrong */ protected CmsParameterConfiguration getComponentsProperties(String location) throws FileNotFoundException, CmsConfigurationException { InputStream stream = null; ZipFile zipFile = null; try { // try to interpret the fileName as a folder File folder = new File(location); // if it is a file it must be a zip-file if (folder.isFile()) { zipFile = new ZipFile(location); ZipEntry entry = zipFile.getEntry(COMPONENTS_PROPERTIES); // path to file might be relative, too if ((entry == null) && location.startsWith("/")) { entry = zipFile.getEntry(location.substring(1)); } if (entry == null) { throw new FileNotFoundException(org.opencms.importexport.Messages.get().getBundle().key( org.opencms.importexport.Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1, location + "/" + COMPONENTS_PROPERTIES)); } stream = zipFile.getInputStream(entry); } else { // it is a folder File file = new File(folder, COMPONENTS_PROPERTIES); stream = new FileInputStream(file); } return new CmsParameterConfiguration(stream); } catch (Throwable ioe) { if (stream != null) { try { stream.close(); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug(e.getLocalizedMessage(), e); } } } if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug(e.getLocalizedMessage(), e); } } } if (ioe instanceof FileNotFoundException) { throw (FileNotFoundException) ioe; } CmsMessageContainer msg = org.opencms.importexport.Messages.get().container( org.opencms.importexport.Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, location + "/" + COMPONENTS_PROPERTIES); if (LOG.isErrorEnabled()) { LOG.error(msg.key(), ioe); } throw new CmsConfigurationException(msg, ioe); } }
From source file:nl.strohalm.cyclos.themes.ThemeHandlerImpl.java
@Override protected void doSelect(final String fileName) { ZipFile zipFile = null; final LocalSettings settings = settingsService.getLocalSettings(); final String charset = settings.getCharset(); try {//from w w w . j a v a2 s .c o m final File file = realFile(fileName); if (!file.exists()) { throw new ThemeNotFoundException(fileName); } zipFile = new ZipFile(file); // Ensure the properties entry exists properties(zipFile); // Find all currently used images by style final Map<String, Collection<String>> imagesByFile = new HashMap<String, Collection<String>>(); final File imageDir = webImageHelper.imagePath(Image.Nature.STYLE); final File[] cssFiles = imageDir.listFiles(STYLE_FILTER); for (final File css : cssFiles) { final String contents = FileUtils.readFileToString(css, charset); final List<String> urls = CSSHelper.resolveURLs(contents); imagesByFile.put(css.getName(), urls); } // Read the files final Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); // We will not handle directories if (entry.isDirectory()) { continue; } final String name = entry.getName(); final String entryFileName = new File(name).getName(); if (name.startsWith("images/")) { final ImageType type = ImageType.getByFileName(entryFileName); // Save the image final Image image = imageService.save(Image.Nature.STYLE, type, entryFileName, zipFile.getInputStream(entry)); // Update the physical image webImageHelper.update(image); } else if (name.startsWith("styles/")) { // Save the style sheet CustomizedFile customizedFile = new CustomizedFile(); customizedFile.setName(entryFileName); customizedFile.setType(CustomizedFile.Type.STYLE); final String contents = IOUtils.toString(zipFile.getInputStream(entry), charset); customizedFile.setContents(contents); final File originalFile = customizationHelper.originalFileOf(Type.STYLE, entryFileName); if (originalFile.exists()) { customizedFile.setOriginalContents(FileUtils.readFileToString(originalFile, charset)); } customizedFile = customizedFileService.saveForTheme(customizedFile); // Update the physical file final File physicalFile = customizationHelper.customizedFileOf(CustomizedFile.Type.STYLE, entryFileName); customizationHelper.updateFile(physicalFile, customizedFile); // Remove images that are no longer used final List<String> newImages = CSSHelper.resolveURLs(contents); final Collection<String> oldImages = imagesByFile.get(entryFileName); if (CollectionUtils.isNotEmpty(oldImages)) { for (final String imageName : oldImages) { if (!newImages.contains(imageName)) { // No longer used. Remove it imageService.removeStyleImage(imageName); // Remove the physical file final File imageFile = new File(imageDir, imageName); customizationHelper.deleteFile(imageFile); } } } } } } catch (final ThemeException e) { throw e; } catch (final Exception e) { throw new ThemeException(e); } finally { try { zipFile.close(); } catch (final Exception e) { // Ignore } } }
From source file:com.hichinaschool.flashcards.async.DeckTask.java
private TaskData doInBackgroundImportReplace(TaskData... params) { // Log.i(AnkiDroidApp.TAG, "doInBackgroundImportReplace"); Collection col = params[0].getCollection(); String path = params[0].getString(); Resources res = AnkiDroidApp.getInstance().getBaseContext().getResources(); // extract the deck from the zip file String fileDir = AnkiDroidApp.getCurrentAnkiDroidDirectory() + "/tmpzip"; File dir = new File(fileDir); if (dir.exists()) { BackupManager.removeDir(dir);/*www . j a v a 2s .c om*/ } publishProgress(new TaskData(res.getString(R.string.import_unpacking))); // from anki2.py String colFile = fileDir + "/collection.anki2"; ZipFile zip; try { zip = new ZipFile(new File(path), ZipFile.OPEN_READ); } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - Error while unzipping: ", e); AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace0"); return new TaskData(false); } if (!Utils.unzipFiles(zip, fileDir, new String[] { "collection.anki2", "media" }, null) || !(new File(colFile)).exists()) { return new TaskData(-2, null, false); } Collection tmpCol = null; try { tmpCol = Storage.Collection(colFile); if (!tmpCol.validCollection()) { tmpCol.close(); return new TaskData(-2, null, false); } } finally { if (tmpCol != null) { tmpCol.close(); } } publishProgress(new TaskData(res.getString(R.string.importing_collection))); String colPath; if (col != null) { // unload collection and trigger a backup colPath = col.getPath(); AnkiDroidApp.closeCollection(true); BackupManager.performBackup(colPath, true); } // overwrite collection colPath = AnkiDroidApp.getCollectionPath(); File f = new File(colFile); f.renameTo(new File(colPath)); int addedCount = -1; try { col = AnkiDroidApp.openCollection(colPath); // because users don't have a backup of media, it's safer to import new // data and rely on them running a media db check to get rid of any // unwanted media. in the future we might also want to duplicate this step // import media HashMap<String, String> nameToNum = new HashMap<String, String>(); HashMap<String, String> numToName = new HashMap<String, String>(); File mediaMapFile = new File(fileDir, "media"); if (mediaMapFile.exists()) { JsonReader jr = new JsonReader(new FileReader(mediaMapFile)); jr.beginObject(); String name; String num; while (jr.hasNext()) { num = jr.nextName(); name = jr.nextString(); nameToNum.put(name, num); numToName.put(num, name); } jr.endObject(); jr.close(); } String mediaDir = col.getMedia().getDir(); int total = nameToNum.size(); int i = 0; for (Map.Entry<String, String> entry : nameToNum.entrySet()) { String file = entry.getKey(); String c = entry.getValue(); File of = new File(mediaDir, file); if (!of.exists()) { Utils.unzipFiles(zip, mediaDir, new String[] { c }, numToName); } ++i; publishProgress(new TaskData(res.getString(R.string.import_media_count, (i + 1) * 100 / total))); } zip.close(); // delete tmp dir BackupManager.removeDir(dir); publishProgress(new TaskData(res.getString(R.string.import_update_counts))); // Update the counts DeckTask.TaskData result = doInBackgroundLoadDeckCounts(new TaskData(col)); if (result == null) { return null; } return new TaskData(addedCount, result.getObjArray(), true); } catch (RuntimeException e) { Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - RuntimeException: ", e); AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace1"); return new TaskData(false); } catch (FileNotFoundException e) { Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - FileNotFoundException: ", e); AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace2"); return new TaskData(false); } catch (IOException e) { Log.e(AnkiDroidApp.TAG, "doInBackgroundImportReplace - IOException: ", e); AnkiDroidApp.saveExceptionReportFile(e, "doInBackgroundImportReplace3"); return new TaskData(false); } }