List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:com.googlecode.jsfFlex.shared.tasks.sdk.UnzipTask.java
protected void performTask() { BufferedOutputStream bufferOutputStream = null; ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(_file)); ZipEntry entry; try {/*www .j a v a2 s .co m*/ while ((entry = zipInputStream.getNextEntry()) != null) { ensureDirectoryExists(entry.getName(), entry.isDirectory()); bufferOutputStream = new BufferedOutputStream(new FileOutputStream(_dest + entry.getName()), BUFFER_SIZE); int currRead = 0; byte[] dataRead = new byte[BUFFER_SIZE]; while ((currRead = zipInputStream.read(dataRead, 0, BUFFER_SIZE)) != -1) { bufferOutputStream.write(dataRead, 0, currRead); } bufferOutputStream.flush(); bufferOutputStream.close(); } _log.debug("UnzipTask performTask has been completed with " + toString()); } catch (IOException ioExcept) { StringBuilder errorMessage = new StringBuilder(); errorMessage.append("Error in Unzip's performTask with following fields \n"); errorMessage.append(toString()); throw new ComponentBuildException(errorMessage.toString(), ioExcept); } finally { try { zipInputStream.close(); if (bufferOutputStream != null) { bufferOutputStream.close(); } } catch (IOException innerIOExcept) { _log.info("Error while closing the streams within UnzipTask's finally block"); } } }
From source file:org.bigbluebuttonproject.fileupload.document.ZipDocumentHandler.java
/** * This method extracts the zip file given to the destDir. It uses ZipFile API * to parse through the files in the zip file. * Only files that the zip file can have are .jpg, .png and .gif formats. * /*w w w . ja v a2 s . c o m*/ * @param fileInput pointing to the zip file * @param destDir directory where extracted files should go */ public void convert(File fileInput, File destDir) { try { // Setup the ZipFile used to read entries ZipFile zf = new ZipFile(fileInput.getAbsolutePath()); // Ensure the extraction directories exist // File directoryStructure = new File(destDir); if (!destDir.exists()) { destDir.mkdirs(); } // Loop through all entries in the zip and extract as necessary ZipEntry currentEntry; for (Enumeration entries = zf.entries(); entries.hasMoreElements();) { currentEntry = (ZipEntry) entries.nextElement(); if (!currentEntry.isDirectory()) { File fileEntry = new File(currentEntry.getName()); String fileName = fileEntry.getName().toLowerCase(); // Make sure to only deal with image files if ((fileName.endsWith(".jpg")) || (fileName.endsWith(".png")) || (fileName.endsWith(".gif"))) { // extracts the corresponding file in dest Directory copyInputStream(zf.getInputStream(currentEntry), new BufferedOutputStream( new FileOutputStream(destDir + File.separator + fileEntry.getName()))); } } } } catch (Exception e) { if (log.isErrorEnabled()) { log.error("Could not load zip document for " + fileInput.getName(), e); } } }
From source file:es.juntadeandalucia.panelGestion.negocio.utiles.file.shape.LocalShapeReader.java
private void unzipShapefiles(File shapefileCompressedData) throws IOException { IOException error = null;/*from w w w . j a v a 2s. c o m*/ if (!shapefilesFolder.exists()) { shapefilesFolder.mkdirs(); } InputStream bis = new FileInputStream(shapefileCompressedData); ZipInputStream zis = new ZipInputStream(bis); ZipEntry ze; try { while ((ze = zis.getNextEntry()) != null) { if (!ze.isDirectory()) { String fileName = ze.getName(); File unzippedFile = new File(shapefilesFolder, fileName); byte[] buffer = new byte[1024]; FileOutputStream fos = new FileOutputStream(unzippedFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); if (fileName.toLowerCase().endsWith(".shp")) { shapefilePath = unzippedFile.toURI(); } } } } catch (IOException e) { error = e; } finally { try { zis.closeEntry(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { zis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (error != null) { throw error; } }
From source file:coral.reef.web.FileUploadController.java
/** * Extracts a zip file specified by the zipFilePath to a directory specified * by destDirectory (will be created if does not exists) * //from www . j a va 2 s . c om * @param zipFilePath * @param destDirectory * @throws IOException */ public void unzip(InputStream zipFilePath, File destDir) throws IOException { if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(zipFilePath); ZipEntry entry = zipIn.getNextEntry(); // iterates over entries in the zip file while (entry != null) { File filePath = new File(destDir, entry.getName()); if (!entry.isDirectory()) { // if the entry is a file, extracts it extractFile(zipIn, filePath); } else { // if the entry is a directory, make the directory filePath.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); }
From source file:jeffaschenk.tomcat.instance.generator.builders.TomcatInstanceBuilderHelper.java
/** * unZip Archive// w w w. ja va2 s .c om * * @param zipFilePath input zip file * @param outputFolder zip file output folder */ protected static void unZipArchive(GenerationLogger GENERATION_LOGGER, String zipFilePath, String outputFolder) throws IOException { /** * Create Output Directory, but should already Exist. */ File folder = new File(outputFolder); if (!folder.exists()) { folder.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); // iterates over entries in the zip file while (entry != null) { String filePath = outputFolder + File.separator + entry.getName(); if (!entry.isDirectory()) { // if the entry is a file, extracts it extractFile(GENERATION_LOGGER, zipIn, filePath); } else { // if the entry is a directory, make the directory File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); }
From source file:fi.mikuz.boarder.gui.ZipImporter.java
private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException { File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { createDir(outputFile);// ww w .j a v a 2s . c o m return; } if (!outputFile.getParentFile().exists()) { createDir(outputFile.getParentFile()); } log = "Extracting: " + entry.getName() + "\n" + log; postMessage("Please wait\n\n" + log); Log.d(TAG, "Extracting: " + entry); BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(inputStream, outputStream); } finally { outputStream.close(); inputStream.close(); } }
From source file:gov.nih.nci.caarray.application.project.FileUploadUtils.java
private void checkForDirectory(ZipEntry entry) throws InvalidFileException { if (entry.isDirectory()) { throw new InvalidFileException(entry.getName(), DIRECTORIES_NOT_SUPPORTED_KEY, result, DIRECTORIES_NOT_SUPPORTED_MESSAGE); }// ww w . j av a 2 s . c om }
From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.action.ArchiveExpander.java
private void expandZip(final File expandDir, final Archive archive, final QcContext context) throws IOException { ZipFile zipFile = null;/* w w w . ja va 2 s.c o m*/ BufferedOutputStream out = null; try { zipFile = new ZipFile(archive.fullArchivePathAndName()); // write each entry to the archive directory final Enumeration zipEnum = zipFile.entries(); boolean foundArchiveDir = false; while (zipEnum.hasMoreElements()) { final ZipEntry entry = (ZipEntry) zipEnum.nextElement(); if (!entry.isDirectory()) { String entryName = entry.getName(); final File entryFile = new File(entryName); // extract out just the filename of the entry if (entryFile.getCanonicalPath().lastIndexOf(File.separator) != -1) { entryName = entryFile.getCanonicalPath() .substring(entryFile.getCanonicalPath().lastIndexOf(File.separator)); } // the file to write is the archive dir plus just the filename final File archiveFile = new File(expandDir, entryName); InputStream in = zipFile.getInputStream(entry); FileOutputStream fout = new FileOutputStream(archiveFile); //noinspection IOResourceOpenedButNotSafelyClosed out = new BufferedOutputStream(fout); copyInputStream(in, out); in = null; out = null; fout.close(); fout = null; } else { // if find a directory that isn't the archive name, add warning for weird archive structure if (!entry.getName().equals(archive.getArchiveName()) && !entry.getName().equals(archive.getArchiveName() + "/")) { context.addWarning(new StringBuilder().append("Archive '") .append("' has a non-standard directory '").append(entry.getName()).append("'.") .append("Archive files should be contained inside a single directory with the archive name as its name.") .toString()); } else { foundArchiveDir = true; } } } if (!foundArchiveDir) { context.addWarning( "Archive files should be contained inside a single directory with the archive name as its name."); } } finally { IOUtils.closeQuietly(out); if (zipFile != null) { zipFile.close(); } } }
From source file:ninja.command.PackageCommand.java
public void unzip(File zipFile, File outputFolder) { byte[] buffer = new byte[1024]; try {/*from ww w . j a v a 2 s. c om*/ // create output directory is not exists File folder = outputFolder; if (!folder.exists()) { folder.mkdir(); } // get the zip file content ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); // get the zipped file list entry ZipEntry ze = zis.getNextEntry(); String zipFilename = zipFile.getName(); while (ze != null) { if (ze.isDirectory()) { ze = zis.getNextEntry(); continue; } String fileName = ze.getName(); File newFile = new File(outputFolder, fileName); String parentFolder = newFile.getParentFile().getName(); // (!"META-INF".equals(parentFolder)) if (newFile.exists() && !"about.html".equals(fileName) && !"META-INF/DEPENDENCIES".equals(fileName) && !"META-INF/LICENSE".equals(fileName) && !"META-INF/NOTICE".equals(fileName) && !"META-INF/NOTICE.txt".equals(fileName) && !"META-INF/MANIFEST.MF".equals(fileName) && !"META-INF/LICENSE.txt".equals(fileName) && !"META-INF/INDEX.LIST".equals(fileName)) { String conflicted = zipManifest.get(newFile.getAbsolutePath()); if (conflicted == null) conflicted = "unknown"; info(String.format("Conflicts for '%s' with '%s'. File alreay exists '%s", zipFilename, conflicted, newFile)); conflicts++; } else zipManifest.put(newFile.getAbsolutePath(), zipFile.getName()); // 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(); count++; } zis.closeEntry(); zis.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:net.ymate.platform.core.beans.impl.DefaultBeanLoader.java
private List<Class<?>> __doFindClassByZip(URL zipUrl, IBeanFilter filter) throws Exception { List<Class<?>> _returnValue = new ArrayList<Class<?>>(); ZipInputStream _zipStream = null; try {/*w w w . j av a 2 s . co m*/ String _zipFilePath = zipUrl.toString(); if (_zipFilePath.indexOf('!') > 0) { _zipFilePath = StringUtils.substringBetween(zipUrl.toString(), "zip:", "!"); } else { _zipFilePath = StringUtils.substringAfter(zipUrl.toString(), "zip:"); } File _zipFile = new File(_zipFilePath); if (!__doCheckExculedFile(_zipFile.getName())) { _zipStream = new ZipInputStream(new FileInputStream(_zipFile)); ZipEntry _zipEntry = null; while (null != (_zipEntry = _zipStream.getNextEntry())) { if (!_zipEntry.isDirectory()) { if (_zipEntry.getName().endsWith(".class") && _zipEntry.getName().indexOf('$') < 0) { String _className = StringUtils.substringBefore(_zipEntry.getName().replace("/", "."), ".class"); __doAddClass(_returnValue, __doLoadClass(_className), filter); } } _zipStream.closeEntry(); } } } finally { if (_zipStream != null) { try { _zipStream.close(); } catch (IOException ignored) { } } } return _returnValue; }