List of usage examples for java.util.zip ZipInputStream getNextEntry
public ZipEntry getNextEntry() throws IOException
From source file:com.nuvolect.securesuite.util.OmniZip.java
public static List<String> getFilesList(OmniFile zipOmni) { ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream()); List<String> arrayList = new ArrayList<>(); ZipEntry entry = null;/*from www. ja va 2s . co m*/ try { while ((entry = zis.getNextEntry()) != null) { arrayList.add(entry.getName()); } zis.close(); } catch (IOException e) { LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e); } return arrayList; }
From source file:com.nuvolect.securesuite.util.OmniZip.java
public static List<String> getDirsList(OmniFile zipOmni) { ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream()); List<String> arrayList = new ArrayList<>(); ZipEntry entry = null;/*w w w.ja va2 s.c o m*/ try { while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) arrayList.add(entry.getName()); } zis.close(); } catch (IOException e) { LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e); } return arrayList; }
From source file:com.plotsquared.iserver.util.FileUtils.java
/** * Add files to a zip file//www .j ava 2 s . com * * @param zipFile Zip File * @param files Files to add to the zip * @param delete If the original files should be deleted * @throws Exception If anything goes wrong */ public static void addToZip(final File zipFile, final File[] files, final boolean delete) throws Exception { Assert.notNull(zipFile, files); if (!zipFile.exists()) { if (!zipFile.createNewFile()) { throw new RuntimeException("Couldn't create " + zipFile); } } final File temporary = File.createTempFile(zipFile.getName(), ""); //noinspection ResultOfMethodCallIgnored temporary.delete(); if (!zipFile.renameTo(temporary)) { throw new RuntimeException("Couldn't rename " + zipFile + " to " + temporary); } final byte[] buffer = new byte[1024 * 16]; // 16mb ZipInputStream zis = new ZipInputStream(new FileInputStream(temporary)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry e = zis.getNextEntry(); while (e != null) { String n = e.getName(); boolean no = true; for (File f : files) { if (f.getName().equals(n)) { no = false; break; } } if (no) { zos.putNextEntry(new ZipEntry(n)); int len; while ((len = zis.read(buffer)) > 0) { zos.write(buffer, 0, len); } } e = zis.getNextEntry(); } zis.close(); for (File file : files) { InputStream in = new FileInputStream(file); zos.putNextEntry(new ZipEntry(file.getName())); int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } zos.closeEntry(); in.close(); } zos.close(); temporary.delete(); if (delete) { for (File f : files) { f.delete(); } } }
From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java
public static void unzipFile(File destFolder, File zipFile) throws IOException { ZipInputStream zipStream = null; try {/* www . j a va 2 s. c o m*/ if (!destFolder.exists()) { destFolder.mkdirs(); } BufferedInputStream in = new BufferedInputStream(new FileInputStream(zipFile)); zipStream = new ZipInputStream(in); ZipEntry entry; while ((entry = zipStream.getNextEntry()) != null) { // get output file String name = entry.getName(); if (name.startsWith("/") || name.startsWith("\\")) name = name.substring(1); File file = new File(destFolder, name); // ensure directory exists File dir = file.getParentFile(); if (!dir.exists()) dir.mkdirs(); IOUtils.copy(zipStream, new FileOutputStream(file)); } } finally { if (zipStream != null) zipStream.close(); } }
From source file:edu.isi.wings.portal.classes.StorageHandler.java
public static String unzipFile(File f, String todirname, String toDirectory) { File todir = new File(toDirectory); if (!todir.exists()) todir.mkdirs();//w w w . j a v a 2s. co m try { // Check if the zip file contains only one directory ZipFile zfile = new ZipFile(f); String topDir = null; boolean isOneDir = true; for (Enumeration<? extends ZipEntry> e = zfile.entries(); e.hasMoreElements();) { ZipEntry ze = e.nextElement(); String name = ze.getName().replaceAll("/.+$", ""); name = name.replaceAll("/$", ""); // OSX Zips carry an extra __MACOSX directory. Ignore it if (name.equals("__MACOSX")) continue; if (topDir == null) topDir = name; else if (!topDir.equals(name)) { isOneDir = false; break; } } zfile.close(); // Delete existing directory (if any) FileUtils.deleteDirectory(new File(toDirectory + File.separator + todirname)); // Unzip file(s) into toDirectory/todirname ZipInputStream zis = new ZipInputStream(new FileInputStream(f)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); // OSX Zips carry an extra __MACOSX directory. Ignore it if (fileName.startsWith("__MACOSX")) { ze = zis.getNextEntry(); continue; } // Get relative file path translated to 'todirname' if (isOneDir) fileName = fileName.replaceFirst(topDir, todirname); else fileName = todirname + File.separator + fileName; // Create directories File newFile = new File(toDirectory + File.separator + fileName); if (ze.isDirectory()) newFile.mkdirs(); else newFile.getParentFile().mkdirs(); try { // Copy file FileOutputStream fos = new FileOutputStream(newFile); IOUtils.copy(zis, fos); fos.close(); String mime = new Tika().detect(newFile); if (mime.equals("application/x-sh") || mime.startsWith("text/")) FileUtils.writeLines(newFile, FileUtils.readLines(newFile)); // Set all files as executable for now newFile.setExecutable(true); } catch (FileNotFoundException fe) { // Silently ignore //fe.printStackTrace(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); return toDirectory + File.separator + todirname; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:edu.umd.cs.marmoset.modelClasses.ZipFileAggregator.java
public static boolean isValidZipfile(byte[] byteArr) { ZipInputStream zipIn = new ZipInputStream(new ByteArrayInputStream(byteArr)); boolean areBytesAZipfile = false; try {/* ww w . j a v a2s. c o m*/ while (true) { ZipEntry entry = zipIn.getNextEntry(); if (entry == null) break; // We need to read at least one valid entry for this to be a valid zipfile areBytesAZipfile = true; } } catch (Throwable e) { return false; } return areBytesAZipfile; }
From source file:JarMaker.java
/** * Unpack the jar file to a directory, till teaf files * @param file The source file name//www . j a v a2 s .c o m * @param file1 The target directory * @author wangxp * @version 2003/11/07 */ public static void unpackAppJar(String file, String file1) throws IOException { // m_log.debug("unpackAppJar begin. file="+file+"|||file1="+file1); BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream); byte abyte0[] = new byte[1024]; for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream .getNextEntry()) { File file2 = new File(file1, zipentry.getName()); if (zipentry.isDirectory()) { if (!file2.exists() && !file2.mkdirs()) throw new IOException("Could not make directory " + file2.getPath()); } else { File file3 = file2.getParentFile(); if (file3 != null && !file3.exists() && !file3.mkdirs()) throw new IOException("Could not make directory " + file3.getPath()); BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file2)); int i; try { while ((i = zipinputstream.read(abyte0, 0, abyte0.length)) != -1) bufferedoutputstream.write(abyte0, 0, i); } catch (IOException ie) { ie.printStackTrace(); // m_logger.error(ie); throw ie; } bufferedoutputstream.close(); } } zipinputstream.close(); bufferedinputstream.close(); // m_log.debug("unpackAppJar end."); }
From source file:Main.java
/** Returns true if these zip files act like equivalent sets. * The order of the zip entries is not important: if they contain * exactly the same contents, this returns true. * @param zip1 one zip file //from www.j a va 2s . c om * @param zip2 another zip file * @return true if the two zip archives are equivalent sets * @throws IOException */ public static boolean zipEquals(File zip1, File zip2) throws IOException { if (zip1.equals(zip2)) return true; InputStream in = null; ZipInputStream zipIn = null; try { in = new FileInputStream(zip1); zipIn = new ZipInputStream(in); ZipEntry e = zipIn.getNextEntry(); Vector<String> entries = new Vector<String>(); while (e != null) { entries.add(e.getName()); InputStream other = getZipEntry(zip2, e.getName()); if (other == null) { return false; } if (equals(zipIn, other) == false) { return false; } e = zipIn.getNextEntry(); } //now we've established everything in zip1 is in zip2 //but what if zip2 has entries zip1 doesn't? zipIn.close(); in.close(); in = new FileInputStream(zip2); zipIn = new ZipInputStream(in); e = zipIn.getNextEntry(); while (e != null) { if (entries.contains(e.getName()) == false) { return false; } e = zipIn.getNextEntry(); } //the entries are exactly the same return true; } finally { try { zipIn.close(); } catch (Throwable t) { } try { in.close(); } catch (Throwable t) { } } }
From source file:com.baasbox.service.dbmanager.DbManagerService.java
public static void importDb(String appcode, ZipInputStream zis) throws FileFormatException, Exception { File newFile = null;/*w ww . j ava 2 s.c o m*/ 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:JarMaker.java
/** * Combine several jar files and some given files into one jar file. * @param outJar The output jar file's filename. * @param inJarsList The jar files to be combined * @param filter A filter that exclude some entries of input jars. User * should implement the EntryFilter interface. * @param inFileList The files to be added into the jar file. * @param prefixs The prefixs of files to be added into the jar file. * inFileList and prefixs should be paired. * @throws FileNotFoundException/*from w w w. ja va2 s .c om*/ * @throws IOException */ @SuppressWarnings("unchecked") public static void combineJars(String outJar, String[] inJarsList, EntryFilter filter, String[] inFileList, String[] prefixs) throws FileNotFoundException, IOException { JarOutputStream jout = new JarOutputStream(new FileOutputStream(outJar)); ArrayList entryList = new ArrayList(); for (int i = 0; i < inJarsList.length; i++) { BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(inJarsList[i])); ZipInputStream zipinputstream = new ZipInputStream(bufferedinputstream); byte abyte0[] = new byte[1024 * 4]; for (ZipEntry zipentry = zipinputstream.getNextEntry(); zipentry != null; zipentry = zipinputstream .getNextEntry()) { if (filter.accept(zipentry)) { if (!entryList.contains(zipentry.getName())) { jout.putNextEntry(zipentry); if (!zipentry.isDirectory()) { int j; try { while ((j = zipinputstream.read(abyte0, 0, abyte0.length)) != -1) jout.write(abyte0, 0, j); } catch (IOException ie) { throw ie; } } entryList.add(zipentry.getName()); } } } zipinputstream.close(); bufferedinputstream.close(); } for (int i = 0; i < inFileList.length; i++) { add(jout, new File(inFileList[i]), prefixs[i]); } jout.close(); }