List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * 2jar?,??jar/*from ww w . j a v a2 s. c o m*/ * @param baseJar * @param diffJar * @param outJar */ public static void unionJar(File baseJar, File diffJar, File outJar) throws IOException { File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } List<String> diffEntries = getZipEntries(diffJar); FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(baseJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory()) { continue; } String name = entry.getName(); if (diffEntries.contains(name)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * jarclass/*from w ww. j a v a 2s . co m*/ * @param inJar * @param removeClasses */ public static void removeFilesFromJar(File inJar, List<String> removeClasses) throws IOException { if (null == removeClasses || removeClasses.isEmpty()) { return; } File outJar = new File(inJar.getParentFile(), inJar.getName() + ".tmp"); File outParentFolder = outJar.getParentFile(); if (!outParentFolder.exists()) { outParentFolder.mkdirs(); } FileOutputStream fos = new FileOutputStream(outJar); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inJar); ZipInputStream zis = new ZipInputStream(fis); try { // loop on the entries of the jar file package and put them in the final jar ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { // do not take directories or anything inside a potential META-INF folder. if (entry.isDirectory() || !entry.getName().endsWith(".class")) { continue; } String name = entry.getName(); String className = getClassName(name); if (removeClasses.contains(className)) { continue; } JarEntry newEntry; // Preserve the STORED method of the input entry. if (entry.getMethod() == JarEntry.STORED) { newEntry = new JarEntry(entry); } else { // Create a new entry so that the compressed len is recomputed. newEntry = new JarEntry(name); } // add the entry to the jar archive jos.putNextEntry(newEntry); // read the content of the entry from the input stream, and write it into the archive. int count; while ((count = zis.read(buffer)) != -1) { jos.write(buffer, 0, count); } // close the entries for this file jos.closeEntry(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); jos.close(); FileUtils.deleteQuietly(inJar); FileUtils.moveFile(outJar, inJar); }
From source file:org.bonitasoft.engine.io.IOUtil.java
private static void extractZipEntries(final ZipInputStream zipInputstream, final File outputFolder) throws IOException { ZipEntry zipEntry; while ((zipEntry = zipInputstream.getNextEntry()) != null) { try {/*from w ww .ja va 2s.c o m*/ // For each entry, a file is created in the output directory "folder" final File outputFile = new File(outputFolder.getAbsolutePath(), zipEntry.getName()); // If the entry is a directory, it creates in the output folder, and we go to the next entry (continue). if (zipEntry.isDirectory()) { mkdirs(outputFile); continue; } writeZipInputToFile(zipInputstream, outputFile); } finally { zipInputstream.closeEntry(); } } }
From source file:gov.nasa.ensemble.resources.ResourceUtil.java
public static IProject unzipProject(String projectName, ZipInputStream zis, IProgressMonitor monitor) throws CoreException, IOException { final String dirPath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath(); if (monitor == null) monitor = new NullProgressMonitor(); try {//from w ww.j a v a 2 s. c o m monitor.beginTask("Loading " + projectName, 300); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { File to = (!new File(dirPath, entry.getName()).getAbsolutePath().contains(projectName)) ? new File(dirPath + File.separator + projectName, entry.getName()) : new File(dirPath, entry.getName()); if (entry.isDirectory()) { if (!to.exists() && !to.mkdirs()) { throw new IOException("Error creating directory: " + to); } } else { File parent = to.getParentFile(); if (parent != null && !parent.exists() && !parent.mkdirs()) { throw new IOException("Error creating directory: " + parent); } FileOutputStream fos = new FileOutputStream(to); try { monitor.subTask("Expanding: " + to.getName()); IOUtils.copy(zis, fos); } finally { IOUtils.closeQuietly(fos); } } monitor.worked(10); } IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); project.create(null); project.open(null); return project; } catch (IOException e) { throw new CoreException( new ExceptionStatus(EnsembleResourcesPlugin.PLUGIN_ID, "adding project to workspace", e)); } finally { zis.close(); monitor.done(); } }
From source file:JarUtils.java
public static void unjar(InputStream in, File dest) throws IOException { if (!dest.exists()) { dest.mkdirs();/*from w w w .j av a2 s. c o m*/ } if (!dest.isDirectory()) { throw new IOException("Destination must be a directory."); } JarInputStream jin = new JarInputStream(in); byte[] buffer = new byte[1024]; ZipEntry entry = jin.getNextEntry(); while (entry != null) { String fileName = entry.getName(); if (fileName.charAt(fileName.length() - 1) == '/') { fileName = fileName.substring(0, fileName.length() - 1); } if (fileName.charAt(0) == '/') { fileName = fileName.substring(1); } if (File.separatorChar != '/') { fileName = fileName.replace('/', File.separatorChar); } File file = new File(dest, fileName); if (entry.isDirectory()) { // make sure the directory exists file.mkdirs(); jin.closeEntry(); } else { // make sure the directory exists File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } // dump the file OutputStream out = new FileOutputStream(file); int len = 0; while ((len = jin.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); out.close(); jin.closeEntry(); file.setLastModified(entry.getTime()); } entry = jin.getNextEntry(); } /* Explicity write out the META-INF/MANIFEST.MF so that any headers such as the Class-Path are see for the unpackaged jar */ Manifest mf = jin.getManifest(); if (mf != null) { File file = new File(dest, "META-INF/MANIFEST.MF"); File parent = file.getParentFile(); if (parent.exists() == false) { parent.mkdirs(); } OutputStream out = new FileOutputStream(file); mf.write(out); out.flush(); out.close(); } jin.close(); }
From source file:com.serotonin.m2m2.Main.java
private static void openZipFiles() throws Exception { ProcessEPoll pep = new ProcessEPoll(); try {//from w w w.jav a 2s. c o m new Thread(pep).start(); File[] zipFiles = new File(new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString()) .listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".zip"); } }); if ((zipFiles == null) || (zipFiles.length == 0)) return; for (File file : zipFiles) { if (!file.isFile()) { continue; } ZipFile zip = new ZipFile(file); try { Properties props = getProperties(zip); String moduleName = props.getProperty("name"); if (moduleName == null) { throw new RuntimeException("name not defined in module properties"); } if (!ModuleUtils.validateName(moduleName)) { throw new RuntimeException(new StringBuilder().append("Module name '").append(moduleName) .append("' is invalid").toString()); } File moduleDir = new File( new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString(), moduleName); if (moduleDir.exists()) LOG.info(new StringBuilder().append("Upgrading module ").append(moduleName).toString()); else { LOG.info(new StringBuilder().append("Installing module ").append(moduleName).toString()); } String persistDirs = props.getProperty("persistPaths"); File moduleSaveDir = new File(new StringBuilder().append(moduleDir).append(".save").toString()); if (!org.apache.commons.lang3.StringUtils.isBlank(persistDirs)) { String[] paths = persistDirs.split(","); for (String path : paths) { path = path.trim(); if (!org.apache.commons.lang3.StringUtils.isBlank(path)) { File from = new File(moduleDir, path); File to = new File(moduleSaveDir, path); if (from.exists()) { if (from.isDirectory()) moveDir(from, to); else { FileUtils.moveFile(from, to); } } } } } deleteDir(moduleDir); if (moduleSaveDir.exists()) moveDir(moduleSaveDir, moduleDir); else { moduleDir.mkdirs(); } Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String name = entry.getName(); File entryFile = new File(moduleDir, name); if (entry.isDirectory()) entryFile.mkdirs(); else { writeFile(entryFile, zip.getInputStream(entry)); } } File moduleWorkDir = new File(moduleDir, "work"); if (moduleWorkDir.exists()) { moveDir(moduleWorkDir, new File(Common.MA_HOME, "work")); } if (HostUtils.isLinux()) { String permissions = props.getProperty("permissions"); if (!org.apache.commons.lang3.StringUtils.isBlank(permissions)) { String[] s = permissions.split(","); for (String permission : s) { setPermission(pep, moduleName, moduleDir, permission); } } } zip.close(); } catch (Exception e) { LOG.warn(new StringBuilder().append("Error while opening zip file ").append(file.getName()) .append(". Is this module built for the core version that you are using? Module ignored.") .toString(), e); } finally { zip.close(); } file.delete(); } } finally { pep.waitForAll(); pep.terminate(); } }
From source file:com.nextep.designer.repository.services.impl.RepositoryUpdaterService.java
/** * Creates the specified delivery (ZIP resource file) on the temporary directory of the local * file system./*from ww w .ja v a2 s. co m*/ * * @param deliveryResource java resource zip file * @return a <code>String</code> representing the absolute path to the delivery root directory. */ private static String createTempDelivery(String deliveryResource) { InputStream is = RepositoryUpdaterService.class.getResourceAsStream(deliveryResource); if (is == null) { throw new ErrorException("Unable to load delivery file: " + deliveryResource); //$NON-NLS-1$ } final String exportLoc = System.getProperty("java.io.tmpdir"); //$NON-NLS-1$ ZipInputStream zipInput = new ZipInputStream(is); ZipEntry entry = null; String rootDeliveryDir = null; try { while ((entry = zipInput.getNextEntry()) != null) { File targetFile = new File(exportLoc, entry.getName()); if (rootDeliveryDir == null) { /* * Initialize the delivery root directory value by searching recursively for the * shallowest directory in the path. */ rootDeliveryDir = getDeliveryRootPath(targetFile, new File(exportLoc)); } if (entry.isDirectory()) { targetFile.mkdirs(); } else { File targetDir = targetFile.getParentFile(); if (!targetDir.exists()) { /* * Creates the directory including any necessary but nonexistent parent * directories. */ targetDir.mkdirs(); } FileOutputStream outFile = new FileOutputStream(targetFile); copyStreams(zipInput, outFile); outFile.close(); } zipInput.closeEntry(); } } catch (IOException e) { throw new ErrorException(e); } finally { try { zipInput.close(); } catch (IOException e) { throw new ErrorException(e); } } return rootDeliveryDir; }
From source file:nl.nn.adapterframework.util.FileUtils.java
public static void unzipStream(InputStream inputStream, File dir) throws IOException { ZipInputStream zis = new ZipInputStream(new BufferedInputStream(inputStream)); try {// w w w . j a v a2 s. com ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { String filename = ze.getName(); File zipFile = new File(dir, filename); if (ze.isDirectory()) { if (!zipFile.exists()) { log.debug("creating directory [" + zipFile.getPath() + "] for ZipEntry [" + ze.getName() + "]"); if (!zipFile.mkdir()) { throw new IOException(zipFile.getPath() + " could not be created"); } } } else { File zipParentFile = zipFile.getParentFile(); if (!zipParentFile.exists()) { log.debug("creating directory [" + zipParentFile.getPath() + "] for ZipEntry [" + ze.getName() + "]"); if (!zipParentFile.mkdir()) { throw new IOException(zipParentFile.getPath() + " could not be created"); } } FileOutputStream fos = new FileOutputStream(zipFile); log.debug("writing ZipEntry [" + ze.getName() + "] to file [" + zipFile.getPath() + "]"); Misc.streamToStream(zis, fos, false); fos.close(); } } } finally { try { zis.close(); } catch (IOException e) { log.warn("exception closing unzip", e); } } }
From source file:com.hazelcast.stabilizer.Utils.java
public static void unzip(byte[] content, final File destinationDir) throws IOException { byte[] buffer = new byte[1024]; ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(content)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File file = new File(destinationDir + File.separator + fileName); // log.finest("Unzipping: " + file.getAbsolutePath()); if (zipEntry.isDirectory()) { file.mkdirs();/*w w w .j a va 2 s .c o m*/ } else { file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); try { int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } finally { closeQuietly(fos); } } zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
From source file:com.aurel.track.lucene.util.FileUtil.java
public static void unzipFile(File uploadZip, File unzipDestinationDirectory) throws IOException { if (!unzipDestinationDirectory.exists()) { unzipDestinationDirectory.mkdirs(); }/*w ww .j a v a2s. co m*/ final int BUFFER = 2048; // Open Zip file for reading ZipFile zipFile = new ZipFile(uploadZip, 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(); 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 = 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); BufferedOutputStream 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); } dest.flush(); dest.close(); is.close(); } } zipFile.close(); }