List of usage examples for java.util.zip ZipInputStream closeEntry
public void closeEntry() throws IOException
From source file:it.cnr.icar.eric.common.Utility.java
/** * * Extracts Zip file contents relative to baseDir * @return An ArrayList containing the File instances for each unzipped file *//*from w w w. j av a 2 s. c om*/ public static ArrayList<File> unZip(String baseDir, InputStream is) throws IOException { ArrayList<File> files = new ArrayList<File>(); ZipInputStream zis = new ZipInputStream(is); while (true) { // Get the next zip entry. Break out of the loop if there are // no more. ZipEntry zipEntry = zis.getNextEntry(); if (zipEntry == null) break; String entryName = zipEntry.getName(); if (FILE_SEPARATOR.equalsIgnoreCase("\\")) { // Convert '/' to Windows file separator entryName = entryName.replaceAll("/", "\\\\"); } String fileName = baseDir + FILE_SEPARATOR + entryName; //Make sure that directory exists. String dirName = fileName.substring(0, fileName.lastIndexOf(FILE_SEPARATOR)); File dir = new File(dirName); dir.mkdirs(); //Entry could be a directory if (!(zipEntry.isDirectory())) { //Entry is a file not a directory. //Write out the content of of entry to file File file = new File(fileName); files.add(file); FileOutputStream fos = new FileOutputStream(file); // Read data from the zip entry. The read() method will return // -1 when there is no more data to read. byte[] buffer = new byte[1000]; int n; while ((n = zis.read(buffer)) > -1) { // In real life, you'd probably write the data to a file. fos.write(buffer, 0, n); } zis.closeEntry(); fos.close(); } else { zis.closeEntry(); } } zis.close(); return files; }
From source file:org.chromium.APKPackager.java
private void extractToFolder(File zipfile, File tempdir) { InputStream inputStream = null; try {//from w w w. j av a 2 s .c om FileInputStream zipStream = new FileInputStream(zipfile); inputStream = new BufferedInputStream(zipStream); ZipInputStream zis = new ZipInputStream(inputStream); inputStream = zis; ZipEntry ze; byte[] buffer = new byte[32 * 1024]; while ((ze = zis.getNextEntry()) != null) { String compressedName = ze.getName(); if (ze.isDirectory()) { File dir = new File(tempdir, compressedName); dir.mkdirs(); } else { File file = new File(tempdir, compressedName); file.getParentFile().mkdirs(); if (file.exists() || file.createNewFile()) { FileOutputStream fout = new FileOutputStream(file); int count; while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); } } zis.closeEntry(); } } catch (Exception e) { Log.e(LOG_TAG, "Unzip error ", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { } } } }
From source file:org.jtrfp.trcl.core.ResourceManager.java
public Font getFont(String zipName, String fontFileName) { try {//from w w w. jav a 2 s . c om zipName = "/fonts/" + zipName; if (zipName.toUpperCase().endsWith(".ZIP")) {// Search the zip ZipInputStream zip = new ZipInputStream(ResourceManager.class.getResourceAsStream(zipName)); ZipEntry entry; while ((entry = zip.getNextEntry()) != null) { System.out.println("ZIP ENTRY: " + entry.getName()); if (entry.getName().toUpperCase().endsWith(fontFileName.toUpperCase())) { Font font = Font.createFont(Font.TRUETYPE_FONT, zip); zip.closeEntry(); zip.close(); return font; } } // end while(elements) } // end if(zip) else { } // TODO: Handle non-zipped fonts? } catch (Exception e) { tr.showStopper(e); } return null; }
From source file:net.sf.sveditor.core.tests.utils.BundleUtils.java
public void unpackBundleZipToFS(String bundle_path, File fs_path) { URL zip_url = fBundle.getEntry(bundle_path); TestCase.assertNotNull(zip_url);//from www. j a v a 2 s . c o m if (!fs_path.isDirectory()) { TestCase.assertTrue(fs_path.mkdirs()); } try { InputStream in = zip_url.openStream(); TestCase.assertNotNull(in); byte tmp[] = new byte[4 * 1024]; int cnt; ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; while ((ze = zin.getNextEntry()) != null) { // System.out.println("Entry: \"" + ze.getName() + "\""); File entry_f = new File(fs_path, ze.getName()); if (ze.getName().endsWith("/")) { // Directory continue; } if (!entry_f.getParentFile().exists()) { TestCase.assertTrue(entry_f.getParentFile().mkdirs()); } FileOutputStream fos = new FileOutputStream(entry_f); BufferedOutputStream bos = new BufferedOutputStream(fos, tmp.length); while ((cnt = zin.read(tmp, 0, tmp.length)) > 0) { bos.write(tmp, 0, cnt); } bos.flush(); bos.close(); fos.close(); zin.closeEntry(); } zin.close(); } catch (IOException e) { e.printStackTrace(); TestCase.fail("Failed to unpack zip file: " + e.getMessage()); } }
From source file:nl.nn.adapterframework.webcontrol.pipes.TestIfsaService.java
private String processZipFile(IPipeLineSession session, InputStream inputStream, String fileEncoding, String applicationId, String serviceId, String messageProtocol) throws IOException { String result = ""; String lastState = null;// w ww.j a v a 2s.co m ZipInputStream archive = new ZipInputStream(inputStream); for (ZipEntry entry = archive.getNextEntry(); entry != null; entry = archive.getNextEntry()) { String name = entry.getName(); int size = (int) entry.getSize(); if (size > 0) { byte[] b = new byte[size]; int rb = 0; int chunk = 0; while (((int) size - rb) > 0) { chunk = archive.read(b, rb, (int) size - rb); if (chunk == -1) { break; } rb += chunk; } String message = XmlUtils.readXml(b, 0, rb, fileEncoding, false); if (StringUtils.isNotEmpty(result)) { result += "\n"; } try { processMessage(applicationId, serviceId, messageProtocol, message); lastState = "success"; } catch (Exception e) { lastState = "error"; } result += name + ":" + lastState; } archive.closeEntry(); } archive.close(); session.put("result", result); return ""; }
From source file:org.geoserver.wfs.response.ShapeZipTest.java
private SimpleFeatureType checkFieldsAreNotEmpty(InputStream in) throws IOException { ZipInputStream zis = new ZipInputStream(in); ZipEntry entry = null;/*from w w w . j a va2 s . co m*/ File tempFolder = createTempFolder("shp_"); String shapeFileName = ""; while ((entry = zis.getNextEntry()) != null) { final String name = entry.getName(); String outName = tempFolder.getAbsolutePath() + File.separatorChar + name; // store .shp file name if (name.toLowerCase().endsWith("shp")) shapeFileName = outName; // copy each file to temp folder FileOutputStream outFile = new FileOutputStream(outName); copyStream(zis, outFile); outFile.close(); zis.closeEntry(); } zis.close(); // create a datastore reading the uncompressed shapefile File shapeFile = new File(shapeFileName); ShapefileDataStore ds = new ShapefileDataStore(shapeFile.toURL()); SimpleFeatureSource fs = ds.getFeatureSource(); SimpleFeatureCollection fc = fs.getFeatures(); SimpleFeatureType schema = fc.getSchema(); SimpleFeatureIterator iter = fc.features(); try { // check that every field has a not null or "empty" value while (iter.hasNext()) { SimpleFeature f = iter.next(); for (Object attrValue : f.getAttributes()) { assertNotNull(attrValue); if (Geometry.class.isAssignableFrom(attrValue.getClass())) assertFalse("Empty geometry", ((Geometry) attrValue).isEmpty()); else assertFalse("Empty value for attribute", attrValue.toString().trim().equals("")); } } } finally { iter.close(); tempFolder.delete(); } return schema; }
From source file:org.tinymediamanager.core.movie.tasks.MovieSubtitleDownloadTask.java
@Override protected void doInBackground() { // let the DownloadTask handle the whole download super.doInBackground(); MediaFile mf = new MediaFile(file); if (mf.getType() != MediaFileType.SUBTITLE) { String basename = FilenameUtils.getBaseName(videoFilePath.toString()) + "." + languageTag; // try to decompress try {/*from w ww. ja v a 2s .c om*/ byte[] buffer = new byte[1024]; ZipInputStream is = new ZipInputStream(new FileInputStream(file.toFile())); // get the zipped file list entry ZipEntry ze = is.getNextEntry(); while (ze != null) { String zipEntryFilename = ze.getName(); String extension = FilenameUtils.getExtension(zipEntryFilename); // check is that is a valid file type if (!Globals.settings.getSubtitleFileType().contains("." + extension) && !"idx".equals(extension)) { ze = is.getNextEntry(); continue; } File destination = new File(file.getParent().toFile(), basename + "." + extension); FileOutputStream os = new FileOutputStream(destination); int len; while ((len = is.read(buffer)) > 0) { os.write(buffer, 0, len); } os.close(); mf = new MediaFile(destination.toPath()); // only take the first subtitle break; } is.closeEntry(); is.close(); Utils.deleteFileSafely(file); } catch (Exception e) { } } mf.gatherMediaInformation(); movie.removeFromMediaFiles(mf); // remove old (possibly same) file movie.addToMediaFiles(mf); // add file, but maybe with other MI values movie.saveToDb(); }
From source file:org.brandroid.openmanager.util.FileManager.java
/** * // w ww. jav a 2s . com * @param zip_file * @param directory */ public void extractZipFiles(OpenPath zip, OpenPath directory) { byte[] data = new byte[BUFFER]; ZipEntry entry; ZipInputStream zipstream; directory.mkdir(); try { zipstream = new ZipInputStream(zip.getInputStream()); while ((entry = zipstream.getNextEntry()) != null) { OpenPath newFile = directory.getChild(entry.getName()); if (!newFile.mkdir()) continue; int read = 0; FileOutputStream out = null; try { out = (FileOutputStream) newFile.getOutputStream(); while ((read = zipstream.read(data, 0, BUFFER)) != -1) out.write(data, 0, read); } catch (Exception e) { Logger.LogError("Error unzipping " + zip.getAbsolutePath(), e); } finally { zipstream.closeEntry(); if (out != null) out.close(); } } } catch (FileNotFoundException e) { Logger.LogError("Couldn't find file.", e); } catch (IOException e) { Logger.LogError("Couldn't extract zip.", e); } }
From source file:org.geoserver.wps.ppio.ShapeZipPPIO.java
@Override public Object decode(InputStream input) throws Exception { // create the temp directory and register it as a temporary resource File tempDir = IOUtils.createTempDirectory("shpziptemp"); // unzip to the temporary directory ZipInputStream zis = null; File shapeFile = null;// ww w . ja v a2 s. c o m try { zis = new ZipInputStream(input); ZipEntry entry = null; while ((entry = zis.getNextEntry()) != null) { String name = entry.getName(); File file = new File(tempDir, entry.getName()); if (entry.isDirectory()) { file.mkdir(); } else { if (file.getName().toLowerCase().endsWith(".shp")) { shapeFile = file; } int count; byte data[] = new byte[4096]; // write the files to the disk FileOutputStream fos = null; try { fos = new FileOutputStream(file); while ((count = zis.read(data)) != -1) { fos.write(data, 0, count); } fos.flush(); } finally { if (fos != null) { fos.close(); } } } zis.closeEntry(); } } finally { if (zis != null) { zis.close(); } } if (shapeFile == null) { FileUtils.deleteDirectory(tempDir); throw new IOException("Could not find any file with .shp extension in the zip file"); } else { ShapefileDataStore store = new ShapefileDataStore(DataUtilities.fileToURL(shapeFile)); resources.addResource(new ShapefileResource(store, tempDir)); return store.getFeatureSource().getFeatures(); } }
From source file:com.denel.facepatrol.MainActivity.java
private void downloadunzip() throws IOException { // for now the file is from assets but will be downloaded later String PATH = getApplicationContext().getDir("pictures", 0).getAbsolutePath() + "/"; InputStream is = getApplicationContext().getAssets().open("ContactsPics.zip"); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is)); try {/*from w w w .j a v a 2 s .co m*/ ZipEntry ze; byte[] buffer = new byte[1024]; while ((ze = zis.getNextEntry()) != null) { if (ze.isDirectory()) { _dirchecker((ze.getName())); } else { FileOutputStream baos = new FileOutputStream(PATH + ze.getName()); BufferedInputStream in = new BufferedInputStream(zis); BufferedOutputStream out = new BufferedOutputStream(baos); int count; while ((count = in.read(buffer)) > 0) { out.write(buffer, 0, count); } zis.closeEntry(); out.close(); } } } finally { zis.close(); } }