List of usage examples for java.util.zip ZipEntry isDirectory
public boolean isDirectory()
From source file:org.cloudfoundry.tools.io.zip.ZipArchive.java
/** * Unzip the specified input stream into a folder. * /*from w ww .ja v a2 s.c o m*/ * @param inputStream the input stream to unzip (this must contain zip contents) * @param destination the destination folder * @see #unpack(File, Folder) */ public static void unpack(InputStream inputStream, Folder destination) { Assert.notNull(inputStream, "InputStream must not be null"); Assert.notNull(destination, "Destination must not be null"); destination.createIfMissing(); ZipInputStream zip = new ZipInputStream(new BufferedInputStream(inputStream)); try { InputStream noCloseZip = new NoCloseInputStream(zip); ZipEntry entry = zip.getNextEntry(); while (entry != null) { if (entry.isDirectory()) { destination.getFolder(entry.getName()).createIfMissing(); } else { destination.getFile(entry.getName()).getContent().write(noCloseZip); } entry = zip.getNextEntry(); } } catch (IOException e) { throw new ResourceException(e); } finally { try { zip.close(); } catch (IOException e) { } } }
From source file:Main.java
public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException { ZipFile zfile = new ZipFile(zipFile); Enumeration<? extends ZipEntry> zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { continue; }/*from ww w . j av a2 s . c o m*/ Log.d(TAG, "ze.getName() = " + ze.getName()); OutputStream os = new BufferedOutputStream( new FileOutputStream(getRealFileName(folderPath, ze.getName()))); 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(); } zfile.close(); return true; }
From source file:Main.java
public static void unZipFolder(InputStream input, String outPathString) throws Exception { java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(input); java.util.zip.ZipEntry zipEntry = null; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName();//www . j a v a 2 s . co m if (zipEntry.isDirectory()) { // get the folder name of the widget szName = szName.substring(0, szName.length() - 1); java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName); folder.mkdirs(); } else { java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName); file.createNewFile(); // get the output stream of the file java.io.FileOutputStream out = new java.io.FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // read (len) bytes into buffer while ((len = inZip.read(buffer)) != -1) { // write (len) byte from buffer at the position 0 out.write(buffer, 0, len); out.flush(); } out.close(); } } //end of while inZip.close(); }
From source file:Main.java
public static int upZipFile(File zipFile, String folderPath) throws IOException { ZipFile zfile = new ZipFile(zipFile); Enumeration zList = zfile.entries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { String dirstr = folderPath + ze.getName(); dirstr = new String(dirstr.getBytes("8859_1"), "GB2312"); File f = new File(dirstr); f.mkdirs();//from w w w .j ava 2 s. c o m continue; } OutputStream os = new BufferedOutputStream( new FileOutputStream(getRealFileName(folderPath, ze.getName()))); 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(); } zfile.close(); return 0; }
From source file:io.inkstand.scribble.rules.ZipAssert.java
/** * Verifies if a folder with the specified path exists * @param zf/*from w w w . j a v a 2s .c o m*/ * the zip file to search for the entry * @param entryPath * the path to the folder entry. Note that it is required that the path ends with '/' otherwise it can't * be recognized as a directory, even though the entry may be found with the trailing slash */ public static void assertZipFolderExists(final ZipFile zf, final String entryPath) { final String folderPath; if (entryPath.endsWith("/")) { folderPath = entryPath; } else { folderPath = entryPath + '/'; } final ZipEntry entry = zf.getEntry(folderPath); assertNotNull("Entry " + folderPath + " does not exist", entry); assertTrue("Entry " + folderPath + " is no folder", entry.isDirectory()); }
From source file:io.inkstand.scribble.rules.ZipAssert.java
/** * Verifies the zip file contains an entry with the specified character content * @param zf/* w ww.ja v a 2 s.c o m*/ * the zip file to be searched * @param entryPath * the path to the entry to be verified, the path must not start with a '/' * @param expectedContent * the content as a string that is expected to be the content of the file. * @throws java.io.IOException */ public static void assertZipContent(final ZipFile zf, final String entryPath, final String expectedContent) throws IOException { final ZipEntry entry = zf.getEntry(entryPath); assertNotNull("Entry " + entryPath + " does not exist", entry); assertFalse("Entry " + entryPath + " is a directory", entry.isDirectory()); try (InputStream is = zf.getInputStream(entry)) { assertNotNull("Entry " + entryPath + " has no content", is); final String actualContent = IOUtils.toString(is); assertEquals(expectedContent, actualContent); } }
From source file:org.carlspring.strongbox.storage.metadata.nuget.TempNupkgFile.java
/** * ZIP attachment is Nuspec XML specification * * @param entry// ww w . ja v a 2 s.c o m * zip attachment * @return true if the attachment matches the attachment with the * specification */ private static boolean isNuspecZipEntry(ZipEntry entry) { return !entry.isDirectory() && entry.getName().endsWith(Nuspec.DEFAULT_FILE_EXTENSION); }
From source file:com.google.gdt.eclipse.designer.gwtext.actions.ConfigureGwtExtOperation.java
private static void extractZip(InputStream zipFile, IFolder targetFolder) throws Exception { ZipInputStream zipInputStream = new ZipInputStream(zipFile); while (true) { ZipEntry zipEntry = zipInputStream.getNextEntry(); if (zipEntry == null) { break; }/* w w w .jav a 2s . c o m*/ if (!zipEntry.isDirectory()) { String entryName = zipEntry.getName(); byte[] byteArray = IOUtils.toByteArray(zipInputStream); IOUtils2.setFileContents(targetFolder.getFile(new Path(entryName)), new ByteArrayInputStream(byteArray)); } zipInputStream.closeEntry(); } zipInputStream.close(); }
From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java
public static File unzip(byte[] zipData, File directory) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(zipData); ZipInputStream zis = new ZipInputStream(bais); ZipEntry entry = zis.getNextEntry(); File root = null;/*from w w w . j a v a 2s . c o m*/ while (entry != null) { if (entry.isDirectory()) { File f = new File(directory, entry.getName()); f.mkdir(); if (root == null) { root = f; } } else { BufferedOutputStream out; out = new BufferedOutputStream(new FileOutputStream(new File(directory, entry.toString())), BUFFER_SIZE); // ZipInputStream can only give us one byte at a time... for (int data = zis.read(); data != -1; data = zis.read()) { out.write(data); } out.close(); } zis.closeEntry(); entry = zis.getNextEntry(); } zis.close(); return root; }
From source file:Main.java
public static byte[] readZipEntry(File zfile, ZipEntry entry) throws ZipException, IOException { Log.d("file3: ", zfile.toString()); Log.d("zipEntry3: ", entry.toString()); ZipFile zipFile = new ZipFile(zfile); if (entry != null && !entry.isDirectory()) { byte[] barr = new byte[(int) entry.getSize()]; int read = 0; int len = 0; InputStream is = zipFile.getInputStream(entry); BufferedInputStream bis = new BufferedInputStream(is); int length = barr.length; while ((len = bis.read(barr, read, length - read)) != -1) { read += len;//w w w. ja v a2s .c om } bis.close(); is.close(); zipFile.close(); return barr; } else { zipFile.close(); return new byte[0]; } }