List of usage examples for org.apache.commons.compress.archivers.zip ZipArchiveEntry isDirectory
public boolean isDirectory()
From source file:jetbrains.exodus.entitystore.BackupTests.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static void extractEntireZip(@NotNull final File zip, @NotNull final File restoreDir) throws IOException { try (ZipFile zipFile = new ZipFile(zip)) { final Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries(); while (zipEntries.hasMoreElements()) { final ZipArchiveEntry zipEntry = zipEntries.nextElement(); final File entryFile = new File(restoreDir, zipEntry.getName()); if (zipEntry.isDirectory()) { entryFile.mkdirs();/*from www . ja v a2 s. com*/ } else { entryFile.getParentFile().mkdirs(); try (FileOutputStream target = new FileOutputStream(entryFile)) { try (InputStream in = zipFile.getInputStream(zipEntry)) { IOUtil.copyStreams(in, target, IOUtil.BUFFER_ALLOCATOR); } } } } } }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
/** * Extracts a {@code *.zip} archive into the target folder. * @param monitor the progress monitor/*from w w w .ja v a2 s . c o m*/ * @param archiveFile the archive file * @param targetDirectory the target folder * @throws IOException if failed to extract the archive */ public static void extractZip(IProgressMonitor monitor, File archiveFile, File targetDirectory) throws IOException { SubMonitor sub = SubMonitor.convert(monitor, Messages.IoUtils_monitorExtractZip, 10); try { ZipFile zip = new ZipFile(archiveFile); try { Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); if (entry.isDirectory()) { createDirectory(targetDirectory, entry); } else { InputStream input = zip.getInputStream(entry); try { File file = createFile(targetDirectory, entry, input); setFileMode(file, entry.getUnixMode()); } finally { input.close(); } sub.worked(1); sub.setWorkRemaining(10); } } } finally { zip.close(); } } finally { if (monitor != null) { monitor.done(); } } }
From source file:com.fjn.helper.common.io.file.zip.zip.ZipArchiveHelper.java
/** * zip/*from w w w . j av a 2s .c o m*/ * * @param zipFile * @param targetDirPath */ public static void upZip(File zipFile, String targetDirPath, String filenameEncoding) { if (!zipFile.exists()) return; File targetDir = FileUtil.ensureDirExists(targetDirPath); ZipFile zipfile = null; try { zipfile = new ZipFile(zipFile, filenameEncoding); ZipArchiveEntry zipEntry = null; InputStream fileIn = null; Enumeration<? extends ZipArchiveEntry> enumer = zipfile.getEntries();// zipfile.entries(); while (enumer.hasMoreElements()) { zipEntry = enumer.nextElement(); if (zipEntry.isDirectory()) { FileUtil.ensureChildDirExists(targetDir, zipEntry.getName()); } else { logger.info("unzip file" + zipEntry.getName()); fileIn = zipfile.getInputStream(zipEntry); File file2 = FileUtil.ensureFileExists(targetDirPath, zipEntry.getName()); FileUtil.copyInputStreamToFile(fileIn, file2); fileIn.close(); } } } catch (Exception e) { e.printStackTrace(); } finally { if (zipfile != null) { try { zipfile.close(); } catch (IOException e) { // ignore it } } } logger.info("?"); }
From source file:mj.ocraptor.extraction.tika.parser.pkg.ZipContainerDetector.java
private static MediaType detectKmz(ZipFile zip) { boolean kmlFound = false; Enumeration<ZipArchiveEntry> entries = zip.getEntries(); while (entries.hasMoreElements()) { ZipArchiveEntry entry = entries.nextElement(); String name = entry.getName(); if (!entry.isDirectory() && name.indexOf('/') == -1 && name.indexOf('\\') == -1) { if (name.endsWith(".kml") && !kmlFound) { kmlFound = true;/*w w w . j ava 2s. co m*/ } else { return null; } } } if (kmlFound) { return MediaType.application("vnd.google-earth.kmz"); } else { return null; } }
From source file:backup.namenode.NameNodeBackupServicePlugin.java
private static ClassLoader getClassLoader(InputStream zipFileInput) throws IOException, FileNotFoundException { File tmpDir = new File(System.getProperty(JAVA_IO_TMPDIR), HDFS_BACKUP_STATUS); File dir = new File(tmpDir, TMP + System.nanoTime()); Closer closer = Closer.create();// w ww . j av a 2 s . c o m closer.register((Closeable) () -> FileUtils.deleteDirectory(dir)); Runtime.getRuntime().addShutdownHook(new Thread(() -> IOUtils.closeQuietly(closer))); dir.mkdirs(); List<File> allFiles = new ArrayList<>(); try (ZipArchiveInputStream zinput = new ZipArchiveInputStream(zipFileInput)) { ZipArchiveEntry zipEntry; while ((zipEntry = zinput.getNextZipEntry()) != null) { String name = zipEntry.getName(); File f = new File(dir, name); if (zipEntry.isDirectory()) { f.mkdirs(); } else { f.getParentFile().mkdirs(); try (FileOutputStream out = new FileOutputStream(f)) { IOUtils.copy(zinput, out); } allFiles.add(f); } } } return new FileClassLoader(allFiles); }
From source file:com.mirth.connect.util.ArchiveUtils.java
/** * Extracts folders/files from a zip archive using zip-optimized code from commons-compress. *//*from ww w .jav a 2 s .c om*/ private static void extractZipArchive(File archiveFile, File destinationFolder) throws CompressException { ZipFile zipFile = null; try { zipFile = new ZipFile(archiveFile); Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); ZipArchiveEntry entry = null; byte[] buffer = new byte[BUFFER_SIZE]; for (; entries.hasMoreElements(); entry = entries.nextElement()) { File outputFile = new File( destinationFolder.getAbsolutePath() + IOUtils.DIR_SEPARATOR + entry.getName()); if (entry.isDirectory()) { FileUtils.forceMkdir(outputFile); } else { InputStream inputStream = zipFile.getInputStream(entry); OutputStream outputStream = FileUtils.openOutputStream(outputFile); try { IOUtils.copyLarge(inputStream, outputStream, buffer); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } } } } catch (Exception e) { throw new CompressException(e); } finally { ZipFile.closeQuietly(zipFile); } }
From source file:abfab3d.io.input.ModelLoader.java
private static void unzipEntry(ZipFile zipFile, ZipArchiveEntry entry, File dest) throws IOException { if (entry.isDirectory()) { createDir(new File(dest, entry.getName())); return;/* ww w . j av a 2 s .c o m*/ } File outputFile = new File(dest, entry.getName()); if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } BufferedInputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { if (outputStream != null) outputStream.close(); if (inputStream != null) inputStream.close(); } }
From source file:com.liferay.blade.cli.command.InstallExtensionCommandTest.java
private static void _testJarsDiff(File warFile1, File warFile2) throws IOException { DifferenceCalculator differenceCalculator = new DifferenceCalculator(warFile1, warFile2); differenceCalculator.setFilenameRegexToIgnore(Collections.singleton(".*META-INF.*")); differenceCalculator.setIgnoreTimestamps(true); Differences differences = differenceCalculator.getDifferences(); if (!differences.hasDifferences()) { return;/*w ww . j a v a 2 s. c om*/ } StringBuilder message = new StringBuilder(); message.append("WAR "); message.append(warFile1); message.append(" and "); message.append(warFile2); message.append(" do not match:"); message.append(System.lineSeparator()); boolean realChange; Map<String, ZipArchiveEntry> added = differences.getAdded(); Map<String, ZipArchiveEntry[]> changed = differences.getChanged(); Map<String, ZipArchiveEntry> removed = differences.getRemoved(); if (added.isEmpty() && !changed.isEmpty() && removed.isEmpty()) { realChange = false; ZipFile zipFile1 = null; ZipFile zipFile2 = null; try { zipFile1 = new ZipFile(warFile1); zipFile2 = new ZipFile(warFile2); for (Map.Entry<String, ZipArchiveEntry[]> entry : changed.entrySet()) { ZipArchiveEntry[] zipArchiveEntries = entry.getValue(); ZipArchiveEntry zipArchiveEntry1 = zipArchiveEntries[0]; ZipArchiveEntry zipArchiveEntry2 = zipArchiveEntries[0]; if (zipArchiveEntry1.isDirectory() && zipArchiveEntry2.isDirectory() && (zipArchiveEntry1.getSize() == zipArchiveEntry2.getSize()) && (zipArchiveEntry1.getCompressedSize() <= 2) && (zipArchiveEntry2.getCompressedSize() <= 2)) { // Skip zipdiff bug continue; } try (InputStream inputStream1 = zipFile1 .getInputStream(zipFile1.getEntry(zipArchiveEntry1.getName())); InputStream inputStream2 = zipFile2 .getInputStream(zipFile2.getEntry(zipArchiveEntry2.getName()))) { List<String> lines1 = StringTestUtil.readLines(inputStream1); List<String> lines2 = StringTestUtil.readLines(inputStream2); message.append(System.lineSeparator()); message.append("--- "); message.append(zipArchiveEntry1.getName()); message.append(System.lineSeparator()); message.append("+++ "); message.append(zipArchiveEntry2.getName()); message.append(System.lineSeparator()); Patch<String> diff = DiffUtils.diff(lines1, lines2); for (Delta<String> delta : diff.getDeltas()) { message.append('\t'); message.append(delta.getOriginal()); message.append(System.lineSeparator()); message.append('\t'); message.append(delta.getRevised()); message.append(System.lineSeparator()); } } realChange = true; break; } } finally { ZipFile.closeQuietly(zipFile1); ZipFile.closeQuietly(zipFile2); } } else { realChange = true; } Assert.assertFalse(message.toString(), realChange); }
From source file:at.treedb.jslib.JsLib.java
/** * Loads an external JavaScript library. * /*from w w w . j av a 2s. com*/ * @param dao * {@code DAOiface} (data access object) * @param name * library name * @param version * optional library version. If this parameter is null the * library with the highest version number will be loaded * @return {@code JsLib} object * @throws Exception */ @SuppressWarnings("resource") public static synchronized JsLib load(DAOiface dao, String name, String version) throws Exception { initJsLib(dao); if (jsLibs == null) { return null; } JsLib lib = null; synchronized (lockObj) { HashMap<Version, JsLib> v = jsLibs.get(name); if (v == null) { return null; } if (version != null) { lib = v.get(new Version(version)); } else { Version[] array = v.keySet().toArray(new Version[v.size()]); Arrays.sort(array); // return the library with the highest version number lib = v.get(array[array.length - 1]); } } if (lib != null) { if (!lib.isExtracted) { // load binary archive data lib.callbackAfterLoad(dao); // detect zip of 7z archive MimeType mtype = ContentInfo.getContentInfo(lib.data); int totalSize = 0; HashMap<String, byte[]> dataMap = null; String libName = "jsLib" + lib.getHistId(); String classPath = lib.getName() + "/java/classes/"; if (mtype != null) { // ZIP archive if (mtype.equals(MimeType.ZIP)) { dataMap = new HashMap<String, byte[]>(); lib.zipInput = new ZipArchiveInputStream(new ByteArrayInputStream(lib.data)); do { ZipArchiveEntry entry = lib.zipInput.getNextZipEntry(); if (entry == null) { break; } if (entry.isDirectory()) { continue; } int size = (int) entry.getSize(); totalSize += size; byte[] data = new byte[size]; lib.zipInput.read(data, 0, size); dataMap.put(entry.getName(), data); if (entry.getName().contains(classPath)) { lib.javaFiles.add(entry.getName()); } } while (true); lib.zipInput.close(); lib.isExtracted = true; // 7-zip archive } else if (mtype.equals(MimeType._7Z)) { dataMap = new HashMap<String, byte[]>(); File tempFile = FileStorage.getInstance().createTempFile(libName, ".7z"); tempFile.deleteOnExit(); Stream.writeByteStream(tempFile, lib.data); lib.sevenZFile = new SevenZFile(tempFile); do { SevenZArchiveEntry entry = lib.sevenZFile.getNextEntry(); if (entry == null) { break; } if (entry.isDirectory()) { continue; } int size = (int) entry.getSize(); totalSize += size; byte[] data = new byte[size]; lib.sevenZFile.read(data, 0, size); dataMap.put(entry.getName(), data); if (entry.getName().contains(classPath)) { lib.javaFiles.add(entry.getName()); } } while (true); lib.sevenZFile.close(); lib.isExtracted = true; } } if (!lib.isExtracted) { throw new Exception("JsLib.load(): No JavaScript archive extracted!"); } // create a buffer for the archive byte[] buf = new byte[totalSize]; int offset = 0; // enumerate the archive entries for (String n : dataMap.keySet()) { byte[] d = dataMap.get(n); System.arraycopy(d, 0, buf, offset, d.length); lib.archiveMap.put(n, new ArchiveEntry(offset, d.length)); offset += d.length; } // create a temporary file containing the extracted archive File tempFile = FileStorage.getInstance().createTempFile(libName, ".dump"); lib.dumpFile = tempFile; tempFile.deleteOnExit(); Stream.writeByteStream(tempFile, buf); FileInputStream inFile = new FileInputStream(tempFile); // closed by the GC lib.inChannel = inFile.getChannel(); // discard the archive data - free the memory lib.data = null; dataMap = null; } } return lib; }
From source file:com.google.dart.tools.update.core.internal.UpdateUtils.java
/** * Unzip a zip file, notifying the given monitor along the way. *//* w w w .j av a2s . c o m*/ public static void unzip(File zipFile, File destination, String taskName, IProgressMonitor monitor) throws IOException { ZipFile zip = new ZipFile(zipFile); //TODO (pquitslund): add real progress units if (monitor != null) { monitor.beginTask(taskName, 1); } Enumeration<ZipArchiveEntry> e = zip.getEntries(); while (e.hasMoreElements()) { ZipArchiveEntry entry = e.nextElement(); File file = new File(destination, entry.getName()); if (entry.isDirectory()) { file.mkdirs(); } else { InputStream is = zip.getInputStream(entry); File parent = file.getParentFile(); if (parent != null && parent.exists() == false) { parent.mkdirs(); } FileOutputStream os = new FileOutputStream(file); try { IOUtils.copy(is, os); } finally { os.close(); is.close(); } file.setLastModified(entry.getTime()); int mode = entry.getUnixMode(); if ((mode & EXEC_MASK) != 0) { file.setExecutable(true); } } } //TODO (pquitslund): fix progress units if (monitor != null) { monitor.worked(1); monitor.done(); } }