List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:Utils.java
/** * Unpack a zip file/*from w w w . j a v a2s .c om*/ * * @param theFile * @param targetDir * @return the file * @throws IOException */ public static File unpackArchive(File theFile, File targetDir) throws IOException { if (!theFile.exists()) { throw new IOException(theFile.getAbsolutePath() + " does not exist"); } if (!buildDirectory(targetDir)) { throw new IOException("Could not create directory: " + targetDir); } ZipFile zipFile = new ZipFile(theFile); for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); File file = new File(targetDir, File.separator + entry.getName()); if (!buildDirectory(file.getParentFile())) { throw new IOException("Could not create directory: " + file.getParentFile()); } if (!entry.isDirectory()) { copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file))); } else { if (!buildDirectory(file)) { throw new IOException("Could not create directory: " + file); } } } zipFile.close(); return theFile; }
From source file:com.mc.printer.model.utils.ZipHelper.java
public static void unZip(String sourceZip, String outDirName) throws IOException { log.info("unzip source:" + sourceZip); log.info("unzip to :" + outDirName); ZipFile zfile = new ZipFile(sourceZip); System.out.println(zfile.getName()); Enumeration zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { //ZipFileZipEntry ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { continue; }/* www. jav a 2 s . c o m*/ //ZipEntry?InputStreamOutputStream File fil = getRealFileName(outDirName, ze.getName()); OutputStream os = new BufferedOutputStream(new FileOutputStream(fil)); InputStream is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } is.close(); os.close(); log.debug("Extracted: " + ze.getName()); } zfile.close(); }
From source file:ZipFileUtil.java
/** * @param zipFile/* w w w. ja v a 2 s . com*/ * @param jiniHomeParentDir */ public static void unzipFileIntoDirectory(ZipFile zipFile, File jiniHomeParentDir) { Enumeration files = zipFile.entries(); File f = null; FileOutputStream fos = null; while (files.hasMoreElements()) { try { ZipEntry entry = (ZipEntry) files.nextElement(); InputStream eis = zipFile.getInputStream(entry); byte[] buffer = new byte[1024]; int bytesRead = 0; f = new File(jiniHomeParentDir.getAbsolutePath() + File.separator + entry.getName()); if (entry.isDirectory()) { f.mkdirs(); continue; } else { f.getParentFile().mkdirs(); f.createNewFile(); } fos = new FileOutputStream(f); while ((bytesRead = eis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); continue; } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { // ignore } } } } }
From source file:it.geosolutions.geofence.gui.server.utility.IoUtility.java
/** * Decompress.// w w w . j a v a2 s . co m * * @param prefix * the prefix * @param inputFile * the input file * @param tempFile * the temp file * @return the file * @throws IOException * Signals that an I/O exception has occurred. */ public static File decompress(final String prefix, final File inputFile, final File tempFile) throws IOException { final File tmpDestDir = createTodayPrefixedDirectory(prefix, new File(tempFile.getParent())); ZipFile zipFile = new ZipFile(inputFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); InputStream stream = zipFile.getInputStream(entry); if (entry.isDirectory()) { // Assume directories are stored parents first then // children. (new File(tmpDestDir, entry.getName())).mkdir(); continue; } File newFile = new File(tmpDestDir, entry.getName()); FileOutputStream fos = new FileOutputStream(newFile); try { byte[] buf = new byte[1024]; int len; while ((len = stream.read(buf)) >= 0) { saveCompressedStream(buf, fos, len); } } catch (IOException e) { zipFile.close(); IOException ioe = new IOException("Not valid ZIP archive file type."); ioe.initCause(e); throw ioe; } finally { fos.flush(); fos.close(); stream.close(); } } zipFile.close(); if ((tmpDestDir.listFiles().length == 1) && (tmpDestDir.listFiles()[0].isDirectory())) { return getShpFile(tmpDestDir.listFiles()[0]); } // File[] files = tmpDestDir.listFiles(new FilenameFilter() { // // public boolean accept(File dir, String name) { // return FilenameUtils.getExtension(name).equalsIgnoreCase("shp"); // } // }); // // return files.length > 0 ? files[0] : null; return getShpFile(tmpDestDir); }
From source file:Main.java
public static void unZip(String zipFile, String outputFolder) throws IOException { byte[] buffer = new byte[1024]; //create output directory is not exists File folder = new File(outputFolder); if (!folder.exists()) { folder.mkdir();/*from w ww . j a v a 2 s.c o m*/ } //get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); //create all non exists folders //else you will hit FileNotFoundException for compressed folder if (ze.isDirectory()) newFile.mkdirs(); else { newFile.getParentFile().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(); }
From source file:com.nuvolect.securesuite.util.OmniZip.java
/** * Unzip the file into the target directory. * The added structure is updated to reflect any new files and directories created. * Overwrite files when requested./* w ww . j a va2 s . c o m*/ * @param zipOmni * @param targetDir * @param added * @param httpIpPort * @return */ public static boolean unzipFile(OmniFile zipOmni, OmniFile targetDir, JsonArray added, String httpIpPort) { String volumeId = zipOmni.getVolumeId(); String rootFolderPath = targetDir.getPath(); boolean DEBUG = true; /** * Keep a list of all directories created. * Defer creating the directory object files until the zip is extracted. * This way the dir=1/0 settings can be set accurately. */ ArrayList<OmniFile> directories = new ArrayList<>(); ZipInputStream zis = new ZipInputStream(zipOmni.getFileInputStream()); ZipEntry entry = null; try { while ((entry = zis.getNextEntry()) != null) { if (entry.isDirectory()) { OmniFile dir = new OmniFile(volumeId, entry.getName()); if (dir.mkdir()) { directories.add(dir); if (DEBUG) LogUtil.log(LogUtil.LogType.OMNI_ZIP, "dir created: " + dir.getPath()); } } else { String path = rootFolderPath + "/" + entry.getName(); OmniFile file = new OmniFile(volumeId, path); // Create any necessary directories file.getParentFile().mkdirs(); if (file.exists()) { file = OmniUtil.makeUniqueName(file); } OutputStream out = file.getOutputStream(); OmniFiles.copyFileLeaveInOpen(zis, out); if (DEBUG) LogUtil.log(LogUtil.LogType.OMNI_ZIP, "file created: " + file.getPath()); if (added != null) added.add(file.getFileObject(httpIpPort)); } } zis.close(); if (added != null) { /** * Iterate over the list of directories created and * create object files for each. * The full tree is now expanded such that the dir=1/0 * can be set accurately. */ for (OmniFile dir : directories) added.add(dir.getFileObject(httpIpPort)); } } catch (IOException e) { LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e); return false; } return true; }
From source file:com.wavemaker.StudioInstallService.java
public static File unzipFile(File zipfile) { int BUFFER = 2048; String zipname = zipfile.getName(); int extindex = zipname.lastIndexOf("."); try {/* www . j ava2 s . com*/ File zipFolder = new File(zipfile.getParentFile(), zipname.substring(0, extindex)); if (zipFolder.exists()) org.apache.commons.io.FileUtils.deleteDirectory(zipFolder); zipFolder.mkdir(); File currentDir = zipFolder; //File currentDir = zipfile.getParentFile(); BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(zipfile.toString()); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { System.out.println("Extracting: " + entry); if (entry.isDirectory()) { File f = new File(currentDir, entry.getName()); if (f.exists()) f.delete(); // relevant if this is the top level folder f.mkdir(); } else { int count; byte data[] = new byte[BUFFER]; //needed for non-dir file ace/ace.js created by 7zip File destFile = new File(currentDir, entry.getName()); // write the files to the disk FileOutputStream fos = new FileOutputStream(currentDir.toString() + "/" + entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } } zis.close(); return currentDir; } catch (Exception e) { e.printStackTrace(); } return (File) null; }
From source file:com.googlecode.osde.internal.profiling.Profile.java
/** * Unzips a zip content into a physical folder. * * @return The newly-created folder with the unzipped content. *///from w w w. j a va2 s . c om private static File unzip(InputStream resource, File output) throws IOException { // Allocate a 16K buffer to read the XPI file faster. ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(resource, 16 * 1024)); ZipEntry entry = zipStream.getNextEntry(); while (entry != null) { final File target = new File(output, entry.getName()); if (entry.isDirectory()) { forceMkdir(target); } else { unzipFile(target, zipStream); } entry = zipStream.getNextEntry(); } return output; }
From source file:de.thischwa.pmcms.tool.compression.Zip.java
/** * Method to extract all {@link ZipInfo}s into 'destDir'. Inner directory structure will be copied. * /*from ww w. jav a2 s . c o m*/ * @param destDir * @param zipInfo * @param monitor must be initialized by the caller. * @throws IOException */ public static void extract(final File destDir, final ZipInfo zipInfo, final IProgressMonitor monitor) throws IOException { if (!destDir.exists()) destDir.mkdirs(); for (String key : zipInfo.getEntryKeys()) { ZipEntry entry = zipInfo.getEntry(key); InputStream in = zipInfo.getInputStream(entry); File entryDest = new File(destDir, entry.getName()); entryDest.getParentFile().mkdirs(); if (!entry.isDirectory()) { OutputStream out = new FileOutputStream(new File(destDir, entry.getName())); try { IOUtils.copy(in, out); out.flush(); if (monitor != null) monitor.worked(1); } finally { // cleanup IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); } } } if (monitor != null) monitor.done(); }
From source file:de.egore911.versioning.deployer.performer.PerformExtraction.java
private static boolean extract(String uri, List<ExtractionPair> extractions) { URL url;// w w w. j av a2 s.c o m try { url = new URL(uri); } catch (MalformedURLException e) { LOG.error("Invalid URI: {}", e.getMessage(), e); return false; } try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); int response = connection.getResponseCode(); int lastSlash = uri.lastIndexOf('/'); if (lastSlash < 0) { LOG.error("Invalid URI: {}", uri); return false; } int lastDot = uri.lastIndexOf('.'); if (lastDot < 0) { LOG.error("Invalid URI: {}", uri); return false; } File downloadFile = File.createTempFile(uri.substring(lastSlash + 1), uri.substring(lastDot + 1)); downloadFile.deleteOnExit(); if (response == HttpURLConnection.HTTP_OK) { try (InputStream in = connection.getInputStream(); FileOutputStream out = new FileOutputStream(downloadFile)) { IOUtils.copy(in, out); } LOG.debug("Downloaded {} to {}", url, downloadFile.getAbsolutePath()); Set<ExtractionPair> usedExtractions = new HashSet<>(); // Perform extractions try (ZipFile zipFile = new ZipFile(downloadFile)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); // Only extract files if (entry.isDirectory()) { continue; } for (ExtractionPair extraction : extractions) { String sourcePattern = extraction.source; if (FilenameUtils.wildcardMatch(entry.getName(), sourcePattern)) { usedExtractions.add(extraction); LOG.debug("Found matching file {} for source pattern {}", entry.getName(), sourcePattern); String filename = getSourcePatternMatch(entry.getName(), sourcePattern); // Workaround: If there is no matcher in 'sourcePattern' it will return the // complete path. Strip it down to the filename if (filename.equals(entry.getName())) { int lastIndexOf = filename.lastIndexOf('/'); if (lastIndexOf >= 0) { filename = filename.substring(lastIndexOf + 1); } } String name = UrlUtil.concatenateUrlWithSlashes(extraction.destination, filename); FileUtils.forceMkdir(new File(name).getParentFile()); try (InputStream in = zipFile.getInputStream(entry); FileOutputStream out = new FileOutputStream(name)) { IOUtils.copy(in, out); } LOG.debug("Extracted {} to {} from {}", entry.getName(), name, uri); } } } } for (ExtractionPair extraction : extractions) { if (!usedExtractions.contains(extraction)) { LOG.debug("Extraction {} to {} not used on {}", extraction.source, extraction.destination, uri); } } return true; } else { LOG.error("Could not download file: {}", uri); return false; } } catch (IOException e) { LOG.error("Could not download file: {}", e.getMessage(), e); return false; } }