List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
public static void unzip(InputStream stream, ArchiveCallback callback, String... matches) { ZipInputStream in = new ZipInputStream(stream); ZipEntry entry; try {/*from w w w .ja v a 2s. c om*/ Map<String, File> skipped = new HashMap<String, File>(); matches = (String[]) ArrayUtils.add(matches, ".*"); while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); if (!entry.isDirectory()) { if (name.contains("/") && !File.separator.equals("/")) name = name.replaceAll("/", File.separator + File.separator); if (name.contains("\\") && !File.separator.equals("\\")) name = name.replaceAll("\\\\", File.separator); String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[0])) { callback.callback(dir, file, in); } else { File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16)); OutputStream out = FileUtils.openOutputStream(path); IOUtils.copy(in, out); IOUtils.closeQuietly(out); skipped.put(name, path); } } } for (int i = 1; i < matches.length; i++) { for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator .hasNext();) { Map.Entry<String, File> next = iterator.next(); String name = next.getKey(); File path = next.getValue(); String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[i])) { iterator.remove(); FileInputStream inputStream = FileUtils.openInputStream(path); callback.callback(dir, file, inputStream); IOUtils.closeQuietly(inputStream); FileUtils.forceDelete(path); } } } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.oeg.oops.VocabUtils.java
/** * Code to unzip a file. Inspired from//from w ww . j av a 2s. c o m * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/ * Taken from * @param resourceName * @param outputFolder */ public static void unZipIt(String resourceName, String outputFolder) { byte[] buffer = new byte[1024]; try { ZipInputStream zis = new ZipInputStream(VocabUtils.class.getResourceAsStream(resourceName)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); System.out.println("file unzip : " + newFile.getAbsoluteFile()); if (ze.isDirectory()) { String temp = newFile.getAbsolutePath(); new File(temp).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { System.err.println("Error while extracting the reosurces: " + ex.getMessage()); } }
From source file:net.firejack.platform.core.utils.ArchiveUtils.java
public static void editzip(InputStream stream, OutputStream outputStream, boolean close, EditArchiveCallback callback, String... matches) { ZipInputStream in = new ZipInputStream(stream); ZipOutputStream zout = new ZipOutputStream(outputStream); ZipEntry entry; try {/* w w w.ja va 2s .co m*/ Map<String, File> skipped = new HashMap<String, File>(); matches = (String[]) ArrayUtils.add(matches, ".*"); while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); if (!entry.isDirectory()) { if (name.contains("/") && !File.separator.equals("/")) name = name.replaceAll("/", File.separator + File.separator); if (name.contains("\\") && !File.separator.equals("\\")) name = name.replaceAll("\\\\", File.separator); String dir = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("^(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[0])) { zout.putNextEntry(new ZipEntry(name)); callback.edit(dir, file, in, zout); zout.closeEntry(); } else { File path = FileUtils.getTempFile(SecurityHelper.generateRandomSequence(16)); OutputStream out = FileUtils.openOutputStream(path); IOUtils.copy(in, out); IOUtils.closeQuietly(out); skipped.put(name, path); } } } for (int i = 1; i < matches.length; i++) { for (Iterator<Map.Entry<String, File>> iterator = skipped.entrySet().iterator(); iterator .hasNext();) { Map.Entry<String, File> next = iterator.next(); String name = next.getKey(); File path = next.getValue(); String dir = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$1"); String file = name.replaceAll("(.*?)(?:\\\\|/?)([~@\\$\\{\\}\\w\\-\\.\\+]+$)", "$2"); if (file.matches(matches[i])) { iterator.remove(); FileInputStream inputStream = FileUtils.openInputStream(path); zout.putNextEntry(entry); callback.edit(dir, file, inputStream, zout); zout.closeEntry(); IOUtils.closeQuietly(inputStream); FileUtils.forceDelete(path); } } } while (callback.add(zout)) { zout.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { if (close) IOUtils.closeQuietly(zout); } }
From source file:com.idocbox.common.file.ZipExtracter.java
/** * extract given zip entry file from zip file * to given destrination directory.// ww w . j av a 2s . co m * @param zf ZipFile object. * @param zipEntry zip entry to extract. * @param desDir destrination directory. * @param startDirLevel the level to start create directory. * Its value is 1,2,... * @throws IOException throw the exception whil desDir is no directory. */ private static void extract(final ZipFile zf, final ZipEntry zipEntry, final String desDir, final int... startDirLevel) throws IOException { //check destination directory. File desf = new File(desDir); if (!desf.exists()) { desf.mkdirs(); } int start = 1; if (null != startDirLevel && startDirLevel.length > 0) { start = startDirLevel[0]; if (start < 1) { start = 1; } } String startDir = ""; String zeName = zipEntry.getName(); String folder = zeName; boolean isDir = zipEntry.isDirectory(); if (null != folder) { String[] folders = folder.split("\\/"); if (null != folders && folders.length > 0) { int len = folders.length; if (start == 1) { startDir = zeName; } else { if (start > len) { //nothing to extract. } else { for (int i = start - 1; i < len; i++) { startDir += "/" + folders[i]; } if (null != startDir) { startDir = startDir.substring(1); } } } } } startDir = StringUtils.trim(startDir); if (StringUtils.isNotEmpty(startDir)) { StringBuilder desFileName = new StringBuilder(desDir); if (!desDir.endsWith("/") && !startDir.startsWith("/")) { desFileName.append("/"); } desFileName.append(startDir); File destFile = new File(desFileName.toString()); if (isDir) {//the entry is a dir. if (!destFile.exists()) { destFile.mkdirs(); } } else {//the entry is a file. File parentDir = new File(destFile.getParentFile().getPath()); if (!parentDir.exists()) { parentDir.mkdirs(); } //get entry input stream. InputStream is = zf.getInputStream(zipEntry); OutputStream os = new FileOutputStream(destFile); IOUtils.copy(is, os); if (null != is) { is.close(); } if (null != os) { os.close(); } } } }
From source file:lucee.commons.io.compress.CompressUtil.java
private static void unzip2(File zipFile, Resource targetDir) throws IOException { ZipFile zf = null;/*from w w w . ja va2 s. c o m*/ try { zf = new ZipFile(zipFile); ZipEntry entry; Enumeration en = zf.entries(); while (en.hasMoreElements()) { entry = (ZipEntry) en.nextElement(); Resource target = targetDir.getRealResource(entry.getName()); if (entry.isDirectory()) { target.mkdirs(); } else { Resource parent = target.getParentResource(); if (!parent.exists()) parent.mkdirs(); InputStream is = zf.getInputStream(entry); IOUtil.copy(is, target, true); } target.setLastModified(entry.getTime()); } } finally { IOUtil.closeEL(zf); } }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
/** * ?jarentry//from w w w . j a v a 2 s . c om * * @param jarFile * @return */ public static List<String> getZipEntries(File jarFile) throws IOException { List<String> entries = new ArrayList<String>(); FileInputStream fis = new FileInputStream(jarFile); 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(); entries.add(name); } } finally { zis.close(); } return entries; }
From source file:com.magnet.tools.tests.ScenarioUtils.java
/** * Utility method to unzip a file/*from w ww . j a va 2 s. c om*/ * * @param file file to unzip * @param outputDir destination directory * @throws IOException if an io exception occurs */ public static void unzip(File file, File outputDir) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(file); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!entryDestination.mkdirs()) { throw new IllegalStateException("Cannot create directory " + entryDestination); } continue; } if (entryDestination.getParentFile().mkdirs()) { throw new IllegalStateException("Cannot create directory " + entryDestination.getParentFile()); } InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); IOUtils.copy(in, out); IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } finally { if (null != zipFile) { try { zipFile.close(); } catch (Exception e) { /* do nothing */ } } } }
From source file:nl.coinsweb.sdk.FileManager.java
/** * Extracts all the content of the .ccr-file specified in the constructor to [TEMP_ZIP_PATH] / [internalRef]. * *//*from www . ja v a2 s . com*/ public static void unzipTo(File sourceFile, Path destinationPath) { byte[] buffer = new byte[1024]; String startFolder = null; try { // Get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(sourceFile)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { if (ze.isDirectory()) { ze = zis.getNextEntry(); continue; } String fileName = ze.getName(); log.info("Dealing with file " + fileName); // If the first folder is a somename/bim/file.ref skip it Path filePath = Paths.get(fileName); Path pathPath = filePath.getParent(); if (pathPath.endsWith("bim") || pathPath.endsWith("bim/repository") || pathPath.endsWith("doc") || pathPath.endsWith("woa")) { Path pathRoot = pathPath.endsWith("repository") ? pathPath.getParent().getParent() : pathPath.getParent(); String prefix = ""; if (pathRoot != null) { prefix = pathRoot.toString(); } if (startFolder == null) { startFolder = prefix; log.debug("File root set to: " + startFolder); } else if (startFolder != null && !prefix.equals(startFolder)) { throw new InvalidContainerFileException( "The container file has an inconsistent file root, was " + startFolder + ", now dealing with " + prefix + "."); } } else { log.debug("Skipping file: " + filePath.toString()); continue; } String insideStartFolder = filePath.toString().substring(startFolder.length()); File newFile = new File(destinationPath + "/" + insideStartFolder); log.info("Extract " + newFile); // Create all non exists folders // else you will hit FileNotFoundException for compressed folder new File(newFile.getParent()).mkdirs(); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
public static void splitZipToFolder(File inputFile, File outputFolder, Set<String> includeEnties) throws IOException { if (!outputFolder.exists()) { outputFolder.mkdirs();//from w w w .j a va2s .c o m } if (null == includeEnties || includeEnties.size() < 1) { return; } final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inputFile); 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 (!includeEnties.contains(name)) { continue; } File destFile = new File(outputFolder, name); destFile.getParentFile().mkdirs(); // read the content of the entry from the input stream, and write it into the archive. int count; FileOutputStream fout = FileUtils.openOutputStream(destFile); while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeEntry(); } } finally { zis.close(); } fis.close(); }
From source file:com.taobao.android.tpatch.utils.JarSplitUtils.java
public static void splitZip(File inputFile, File outputFile, Set<String> includeEnties) throws IOException { if (outputFile.exists()) { FileUtils.deleteQuietly(outputFile); }//from w ww .ja va 2s . com if (null == includeEnties || includeEnties.size() < 1) { return; } FileOutputStream fos = new FileOutputStream(outputFile); ZipOutputStream jos = new ZipOutputStream(fos); final byte[] buffer = new byte[8192]; FileInputStream fis = new FileInputStream(inputFile); 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 (!includeEnties.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(); }