List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:com.baasbox.service.dbmanager.DbManagerService.java
public static void importDb(String appcode, ZipInputStream zis) throws FileFormatException, Exception { File newFile = null;// www . ja v a2 s . c om FileOutputStream fout = null; try { //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); if (ze == null) throw new FileFormatException("Looks like the uploaded file is not a valid export."); if (ze.isDirectory()) { ze = zis.getNextEntry(); } if (ze != null) { newFile = File.createTempFile("export", ".json"); fout = new FileOutputStream(newFile); IOUtils.copy(zis, fout, BBConfiguration.getImportExportBufferSize()); fout.close(); } else { throw new FileFormatException("Looks like the uploaded file is not a valid export."); } ZipEntry manifest = zis.getNextEntry(); if (manifest != null) { File manifestFile = File.createTempFile("manifest", ".txt"); fout = new FileOutputStream(manifestFile); for (int c = zis.read(); c != -1; c = zis.read()) { fout.write(c); } fout.close(); String manifestContent = FileUtils.readFileToString(manifestFile); manifestFile.delete(); Pattern p = Pattern.compile(BBInternalConstants.IMPORT_MANIFEST_VERSION_PATTERN); Matcher m = p.matcher(manifestContent); if (m.matches()) { String version = m.group(1); if (version.compareToIgnoreCase("0.6.0") < 0) { //we support imports from version 0.6.0 throw new FileFormatException(String.format( "Current baasbox version(%s) is not compatible with import file version(%s)", BBConfiguration.getApiVersion(), version)); } else { if (BaasBoxLogger.isDebugEnabled()) BaasBoxLogger.debug("Version : " + version + " is valid"); } } else { throw new FileFormatException("The manifest file does not contain a version number"); } } else { throw new FileFormatException("Looks like zip file does not contain a manifest file"); } if (newFile != null) { DbHelper.importData(appcode, newFile); zis.closeEntry(); zis.close(); } else { throw new FileFormatException("The import file is empty"); } } catch (FileFormatException e) { BaasBoxLogger.error(ExceptionUtils.getMessage(e)); throw e; } catch (Throwable e) { BaasBoxLogger.error(ExceptionUtils.getStackTrace(e)); throw new Exception("There was an error handling your zip import file.", e); } finally { try { if (zis != null) { zis.close(); } if (fout != null) { fout.close(); } } catch (IOException e) { // Nothing to do here } } }
From source file:Main.java
public static String unzip(String filename) throws IOException { BufferedOutputStream origin = null; String path = null;/*from w w w .j a va2s .c o m*/ Integer BUFFER_SIZE = 20480; if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { File flockedFilesFolder = new File( Environment.getExternalStorageDirectory() + File.separator + "FlockLoad"); System.out.println("FlockedFileDir: " + flockedFilesFolder); String compressedFile = flockedFilesFolder.toString() + "/" + filename; System.out.println("compressed File name:" + compressedFile); //String uncompressedFile = flockedFilesFolder.toString()+"/"+"flockUnZip"; File purgeFile = new File(compressedFile); try { ZipInputStream zin = new ZipInputStream(new FileInputStream(compressedFile)); BufferedInputStream bis = new BufferedInputStream(zin); try { ZipEntry ze = null; while ((ze = zin.getNextEntry()) != null) { byte data[] = new byte[BUFFER_SIZE]; path = flockedFilesFolder.toString() + "/" + ze.getName(); System.out.println("path is:" + path); if (ze.isDirectory()) { File unzipFile = new File(path); if (!unzipFile.isDirectory()) { unzipFile.mkdirs(); } } else { FileOutputStream fout = new FileOutputStream(path, false); origin = new BufferedOutputStream(fout, BUFFER_SIZE); try { /* for (int c = bis.read(data, 0, BUFFER_SIZE); c != -1; c = bis.read(data)) { origin.write(c); } */ int count; while ((count = bis.read(data, 0, BUFFER_SIZE)) != -1) { origin.write(data, 0, count); } zin.closeEntry(); } finally { origin.close(); fout.close(); } } } } finally { bis.close(); zin.close(); purgeFile.delete(); } } catch (Exception e) { e.printStackTrace(); } return path; } return null; }
From source file:fr.inria.atlanmod.neoemf.benchmarks.datastore.helper.BackendHelper.java
private static List<String> getZipResources() throws IOException { if (isNull(AVAILABLE_RESOURCES)) { AVAILABLE_RESOURCES = new ArrayList<>(); try (ZipInputStream inputStream = new ZipInputStream( BackendHelper.class.getResourceAsStream("/" + ZIP_FILENAME))) { ZipEntry entry = inputStream.getNextEntry(); while (nonNull(entry)) { if (!entry.isDirectory() && checkValidResource(entry.getName())) { AVAILABLE_RESOURCES.add(new File(entry.getName()).getName()); }/*from w w w .ja va 2 s. co m*/ inputStream.closeEntry(); entry = inputStream.getNextEntry(); } } } return AVAILABLE_RESOURCES; }
From source file:fr.inria.atlanmod.neoemf.benchmarks.datastore.helper.BackendHelper.java
private static File extractFromZip(String filename, Path outputDir) throws IOException { File outputFile = null;// w w w. jav a 2 s.c o m boolean fileFound = false; try (ZipInputStream inputStream = new ZipInputStream( BackendHelper.class.getResourceAsStream("/" + ZIP_FILENAME))) { ZipEntry entry = inputStream.getNextEntry(); while (nonNull(entry) || !fileFound) { if (!entry.isDirectory() && Objects.equals(new File(entry.getName()).getName(), filename)) { outputFile = extractEntryFromZip(inputStream, entry, outputDir); fileFound = true; } inputStream.closeEntry(); entry = inputStream.getNextEntry(); } } return outputFile; }
From source file:com.alibaba.antx.util.ZipUtil.java
/** * /* w w w .j a v a 2 s . c o m*/ * * @param todir * @param zipStream ? * @param zipEntry zip * @param overwrite ? * @throws IOException Zip? */ protected static void extractFile(File todir, InputStream zipStream, ZipEntry zipEntry, boolean overwrite) throws IOException { String entryName = zipEntry.getName(); Date entryDate = new Date(zipEntry.getTime()); boolean isDirectory = zipEntry.isDirectory(); File targetFile = FileUtil.getFile(todir, entryName); if (!overwrite && targetFile.exists() && targetFile.lastModified() >= entryDate.getTime()) { log.debug("Skipping " + targetFile + " as it is up-to-date"); return; } log.info("expanding " + entryName + " to " + targetFile); if (isDirectory) { targetFile.mkdirs(); } else { File dir = targetFile.getParentFile(); dir.mkdirs(); byte[] buffer = new byte[8192]; int length = 0; OutputStream ostream = null; try { ostream = new BufferedOutputStream(new FileOutputStream(targetFile), 8192); while ((length = zipStream.read(buffer)) >= 0) { ostream.write(buffer, 0, length); } } finally { if (ostream != null) { try { ostream.close(); } catch (IOException e) { } } } } targetFile.setLastModified(entryDate.getTime()); }
From source file:de.rub.syssec.saaf.analysis.steps.extract.ApkUnzipper.java
/** * Extracts the given archive into the given destination directory * /* w ww . j ava 2s. c o m*/ * @param archive * - the file to extract * @param dest * - the destination directory * @throws Exception */ public static void extractArchive(File archive, File destDir) throws IOException { if (!destDir.exists()) { destDir.mkdir(); } ZipFile zipFile = new ZipFile(archive); Enumeration<? extends ZipEntry> entries = zipFile.entries(); byte[] buffer = new byte[16384]; int len; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); String entryFileName = entry.getName(); File dir = buildDirectoryHierarchyFor(entryFileName, destDir); if (!dir.exists()) { dir.mkdirs(); } if (!entry.isDirectory()) { File file = new File(destDir, entryFileName); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); while ((len = bis.read(buffer)) > 0) { bos.write(buffer, 0, len); } bos.flush(); bos.close(); bis.close(); } } zipFile.close(); }
From source file:com.frostwire.util.ZipUtils.java
private static void unzipEntries(File folder, ZipInputStream zis, int itemCount, long time, ZipListener listener) throws IOException, FileNotFoundException { ZipEntry ze = null; int item = 0; while ((ze = zis.getNextEntry()) != null) { item++;//from w w w . jav a 2 s.c om String fileName = ze.getName(); File newFile = new File(folder, fileName); LOG.debug("unzip: " + newFile.getAbsoluteFile()); if (ze.isDirectory()) { if (!newFile.mkdirs()) { break; } continue; } if (listener != null) { int progress = (item == itemCount) ? 100 : (int) (((double) (item * 100)) / (double) (itemCount)); listener.onUnzipping(fileName, progress); } FileOutputStream fos = new FileOutputStream(newFile); try { int n; byte[] buffer = new byte[1024]; while ((n = zis.read(buffer)) > 0) { fos.write(buffer, 0, n); if (listener != null && listener.isCanceled()) { // not the best way throw new IOException("Uncompress operation cancelled"); } } } finally { fos.close(); zis.closeEntry(); } newFile.setLastModified(time); } }
From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java
private static void extractEntry(final ZipFile zf, final ZipEntry entry, final File destDir) throws IOException { File file = new File(destDir, entry.getName()); if (entry.isDirectory()) { file.mkdirs();//from www . ja v a 2s . c o m } else { new File(file.getParent()).mkdirs(); try (InputStream is = zf.getInputStream(entry); FileOutputStream os = new FileOutputStream(file)) { copy(Channels.newChannel(is), os.getChannel()); } // preserve modification time; must be set after the stream is closed file.setLastModified(entry.getTime()); } }
From source file:com.t3.persistence.FileUtil.java
public static void unzipFile(File sourceFile, File destDir) throws IOException { if (!sourceFile.exists()) throw new IOException("source file does not exist: " + sourceFile); try (ZipFile zipFile = new ZipFile(sourceFile)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) continue; String path = destDir.getAbsolutePath() + File.separator + entry.getName(); File file = new File(path); file.getParentFile().mkdirs(); //System.out.println("Writing file: " + path); try (InputStream is = zipFile.getInputStream(entry); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path));) { IOUtils.copy(is, os);//from w ww. j a v a 2 s.co m } } } }
From source file:io.card.development.recording.Recording.java
private static Hashtable<String, byte[]> unzipFiles(InputStream recordingStream) throws IOException { ZipEntry zipEntry; ZipInputStream zis = new ZipInputStream(recordingStream); Hashtable<String, byte[]> fileHash = new Hashtable<String, byte[]>(); byte[] buffer = new byte[512]; while ((zipEntry = zis.getNextEntry()) != null) { if (!zipEntry.isDirectory()) { int read = 0; ByteArrayOutputStream fileStream = new ByteArrayOutputStream(); do {/*from w w w .j a v a 2 s . c o m*/ read = zis.read(buffer, 0, buffer.length); if (read != -1) { fileStream.write(buffer, 0, read); } } while (read != -1); byte[] fileData = fileStream.toByteArray(); fileHash.put(zipEntry.getName(), fileData); } } return fileHash; }