List of usage examples for java.util.zip ZipFile OPEN_READ
int OPEN_READ
To view the source code for java.util.zip ZipFile OPEN_READ.
Click Source Link
From source file:it.readbeyond.minstrel.librarian.FormatHandlerEPUB.java
public Publication parseFile(File file) { Publication p = super.parseFile(file); Format format = new Format(EPUB_FORMAT); pathThumbnailSourceRelativeToOPF = null; coverManifestItemID = null;/* w w w . ja va2 s .co m*/ String OPFPath = this.getOPFPath(file); if (OPFPath != null) { try { ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ); ZipEntry OPFEntry = zipFile.getEntry(OPFPath); if (OPFEntry != null) { InputStream is; // first pass: parse metadata is = zipFile.getInputStream(OPFEntry); parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(is, null); parser.nextTag(); this.parsePackage(format, 1); is.close(); // second pass: parse manifest is = zipFile.getInputStream(OPFEntry); parser = Xml.newPullParser(); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); parser.setInput(is, null); parser.nextTag(); this.parsePackage(format, 2); is.close(); // item is valid p.isValid(true); // set the cover path, relative to the EPUB container (aka ZIP) root if (pathThumbnailSourceRelativeToOPF != null) { // concatenate OPFPath parent directory and pathThumbnailSourceRelativeToOPF File tmp = new File(new File(OPFPath).getParent(), pathThumbnailSourceRelativeToOPF); // remove leading "/" format.addMetadatum("internalPathCover", tmp.getAbsolutePath().substring(1)); } } zipFile.close(); } catch (Exception e) { // invalidate item, so it will not be added to library p.isValid(false); } } p.addFormat(format); // extract cover super.extractCover(file, format, p.getID()); return p; }
From source file:org.kuali.rice.kew.plugin.ZipFilePluginLoader.java
/** * Extracts the plugin files if necessary. *//*from w w w. j a va 2 s. co m*/ protected void extractPluginFiles() throws Exception { if (isExtractNeeded()) { // first, delete the current copy of the extracted plugin if (extractionDirectory.exists()) { // TODO how to handle locked files under windows?!? This will throw an IOException in this case. FileUtils.deleteDirectory(extractionDirectory); } if (!extractionDirectory.mkdir()) { throw new WorkflowRuntimeException("Could not create the extraction directory for the plugin: " + extractionDirectory.getAbsolutePath()); } ZipFile zipFile = new ZipFile(pluginZipFile, ZipFile.OPEN_READ); for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); File entryFile = new File(extractionDirectory + java.io.File.separator + entry.getName()); if (entry.isDirectory()) { // if its a directory, create it if (!entryFile.mkdir()) { throw new WorkflowRuntimeException( "Failed to create directory: " + entryFile.getAbsolutePath()); } continue; } InputStream is = null; OutputStream os = null; try { is = new BufferedInputStream(zipFile.getInputStream(entry)); // get the input stream os = new BufferedOutputStream(new FileOutputStream(entryFile)); while (is.available() > 0) { // write contents of 'is' to 'fos' os.write(is.read()); } } finally { if (os != null) { os.close(); } if (is != null) { is.close(); } } } } }
From source file:eionet.gdem.utils.ZipUtil.java
/** * Unzips files to output directory./* w w w .j a v a 2 s .co m*/ * @param inZip Zip file * @param outDir Output directory * @throws IOException If an error occurs. */ public static void unzip(String inZip, String outDir) throws IOException { File sourceZipFile = new File(inZip); File unzipDestinationDirectory = new File(outDir); // Open Zip file for reading ZipFile zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ); // Create an enumeration of the entries in the zip file Enumeration zipFileEntries = zipFile.entries(); // Process each entry while (zipFileEntries.hasMoreElements()) { // grab a zip file entry ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); String currentEntry = entry.getName(); // System.out.println("Extracting: " + entry); File destFile = new File(unzipDestinationDirectory, currentEntry); // grab file's parent directory structure File destinationParent = destFile.getParentFile(); // create the parent directory structure if needed destinationParent.mkdirs(); // extract file if not a directory if (!entry.isDirectory()) { BufferedInputStream is = null; BufferedOutputStream dest = null; try { is = new BufferedInputStream(zipFile.getInputStream(entry)); int currentByte; // establish buffer for writing file byte[] data = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destFile); dest = new BufferedOutputStream(fos, BUFFER); // read and write until last byte is encountered while ((currentByte = is.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, currentByte); } } finally { IOUtils.closeQuietly(dest); IOUtils.closeQuietly(is); } } } zipFile.close(); }
From source file:com.ichi2.libanki.sync.RemoteMediaServer.java
/** * args: files/*from ww w . j av a 2s. co m*/ * <br> * This method returns a ZipFile with the OPEN_DELETE flag, ensuring that the file on disk will * be automatically deleted when the stream is closed. */ public ZipFile downloadFiles(List<String> top) throws UnknownHttpResponseException { try { HttpResponse resp; resp = super.req("downloadFiles", super.getInputStream(Utils.jsonToString(new JSONObject().put("files", new JSONArray(top))))); String zipPath = mCol.getPath().replaceFirst("collection\\.anki2$", "tmpSyncFromServer.zip"); // retrieve contents and save to file on disk: super.writeToFile(resp.getEntity().getContent(), zipPath); return new ZipFile(new File(zipPath), ZipFile.OPEN_READ | ZipFile.OPEN_DELETE); } catch (JSONException e) { throw new RuntimeException(e); } catch (IOException e) { Timber.e(e, "Failed to download requested media files"); throw new RuntimeException(e); } }
From source file:it.readbeyond.minstrel.librarian.FormatHandlerAbstractZIP.java
protected List<ZipAsset> getSortedListOfAssets(String path, String[] allowedExtensions) { List<ZipAsset> assets = new ArrayList<ZipAsset>(); try {//from w w w.j a v a2 s . co m // read assets ZipFile zipFile = new ZipFile(new File(path), ZipFile.OPEN_READ); Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); while (zipFileEntries.hasMoreElements()) { ZipEntry ze = zipFileEntries.nextElement(); String name = ze.getName(); String lower = name.toLowerCase(); if (this.fileHasAllowedExtension(lower, allowedExtensions)) { assets.add(new ZipAsset(name, null)); } } zipFile.close(); // sort assets Collections.sort(assets); } catch (Exception e) { // nop } return assets; }
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();/*from w ww . jav a 2 s . 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:it.readbeyond.minstrel.librarian.FormatHandlerABZ.java
private List<ZipAsset> parseM3UPlaylistEntry(String path, String playlistEntry) { List<ZipAsset> assets = new ArrayList<ZipAsset>(); try {/*from www . j a v a 2s . c o m*/ ZipFile zipFile = new ZipFile(new File(path), ZipFile.OPEN_READ); ZipEntry ze = zipFile.getEntry(playlistEntry); String text = this.getZipEntryText(zipFile, ze); String[] lines = text.split("\n"); // check that the first line starts with the M3U header if ((lines.length > 0) && (lines[0].startsWith(ABZ_M3U_HEADER))) { for (int i = 1; i < lines.length; i++) { String line = lines[i].trim(); // if line starts with the M3U preamble, parse it if (line.startsWith(ABZ_M3U_LINE_PREAMBLE)) { HashMap<String, String> meta = new HashMap<String, String>(); // get track duration and title Matcher m = ABZ_M3U_LINE_PATTERN.matcher(line); if (m.find()) { meta.put("duration", m.group(1)); meta.put("title", m.group(2)); } String line2 = lines[i + 1].trim(); // generate entry path File w = new File(new File(playlistEntry).getParent(), line2); line2 = w.getAbsolutePath().substring(1); // add asset assets.add(new ZipAsset(line2, meta)); // go to the next pair of lines i += 1; } } } // close ZIP zipFile.close(); } catch (Exception e) { // nothing } return assets; }
From source file:org.acdd.ext.bundleInfo.maker.PackageLite.java
public static PackageLite parse(String apkPath) { ZipFile file = null;/*from w ww . j av a 2s . c o m*/ StringBuilder xmlSb = new StringBuilder(100); try { File apkFile = new File(apkPath); file = new ZipFile(apkFile, ZipFile.OPEN_READ); ZipEntry entry = file.getEntry(DEFAULT_XML); AXmlResourceParser parser = new AXmlResourceParser(); parser.open(file.getInputStream(entry)); PackageLite packageLite = new PackageLite(); packageLite.apkMD5 = ACDDFileUtils.getMD5(apkPath); packageLite.size = apkFile.length(); packageLite.checkNativeLibs(file); packageLite.parse(parser); //System.err.println(packageLite.getBundleInfo().toString()); file.close(); return packageLite; //parser.close(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.openAtlas.bundleInfo.maker.PackageLite.java
public static PackageLite parse(String apkPath) { ZipFile file = null;/*from w w w .j ava2 s. co m*/ StringBuilder xmlSb = new StringBuilder(100); try { File apkFile = new File(apkPath); file = new ZipFile(apkFile, ZipFile.OPEN_READ); ZipEntry entry = file.getEntry(DEFAULT_XML); AXmlResourceParser parser = new AXmlResourceParser(); parser.open(file.getInputStream(entry)); PackageLite packageLite = new PackageLite(); packageLite.apkMD5 = FileUtils.getMD5(apkPath); packageLite.size = apkFile.length(); packageLite.checkNativeLibs(file); packageLite.parse(parser); //System.err.println(packageLite.getBundleInfo().toString()); file.close(); return packageLite; //parser.close(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:it.readbeyond.minstrel.librarian.FormatHandlerCBZ.java
private List<ZipAsset> parseImagePlaylistEntry(String path, String playlistEntry) { List<ZipAsset> assets = new ArrayList<ZipAsset>(); try {//from w ww .j a va 2 s. com ZipFile zipFile = new ZipFile(new File(path), ZipFile.OPEN_READ); ZipEntry ze = zipFile.getEntry(playlistEntry); String text = this.getZipEntryText(zipFile, ze); String[] lines = text.split("\n"); // if we have lines if (lines.length > 0) { for (int i = 0; i < lines.length; i++) { String line = lines[i].trim(); // do we have a file name? if ((line.length() > 0) && (!line.startsWith("#"))) { HashMap<String, String> meta = new HashMap<String, String>(); // duration if (i + 1 < lines.length) { String line2 = lines[i + 1].trim(); if ((line2.length() > 0) && (!line2.startsWith("#"))) { i += 1; meta.put("duration", line2); // title if (i + 1 < lines.length) { String line3 = lines[i + 1].trim(); if ((line3.length() > 0) && (!line3.startsWith("#"))) { i += 1; meta.put("title", line3); } } } } // generate entry path File w = new File(new File(playlistEntry).getParent(), line); line = w.getAbsolutePath().substring(1); // add asset assets.add(new ZipAsset(line, meta)); } else { // either comment or blank line => continue } } } // close ZIP zipFile.close(); } catch (Exception e) { // nothing } return assets; }