private static List<String> getZipFileList(File file) { List<String> filesList = new ArrayList<>(); try (ZipFile zipFile = new ZipFile(file)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); filesList.add(entry.getName()); } } catch (Exception e) { LOG.error("Error read zip file '{}'", file.getAbsolutePath(), e); } return filesList; }
private static ByteBuffer getZipEntryData(ZipFile zf, String entryPath) throws IOException { final ZipEntry entry = zf.getEntry(entryPath); InputStream is = null; try { is = new BufferedInputStream(zf.getInputStream(entry)); final byte[] data = Utils.toByteArray(is); return ByteBuffer.wrap(data); } finally { if (is != null) { try { is.close(); } catch (Throwable ignored) { // Ignored. } } } }
/** * Return the files' comment in ZIP file. * * @param zipFile The ZIP file. * @return the files' comment in ZIP file * @throws IOException if an I/O error has occurred */ public static List<String> getComments(final File zipFile) throws IOException { if (zipFile == null) return null; List<String> comments = new ArrayList<>(); ZipFile zip = new ZipFile(zipFile); Enumeration<?> entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = ((ZipEntry) entries.nextElement()); comments.add(entry.getComment()); } zip.close(); return comments; }
private InputStream getFromJar (String jarFile, String sharedLibrary) throws IOException { ZipFile file = new ZipFile(nativesJar); ZipEntry entry = file.getEntry(sharedLibrary); return file.getInputStream(entry); }
private static boolean isZipFileCanBeOpen(File file) { try (ZipFile zipFile = new ZipFile(file)) { return zipFile.entries().hasMoreElements(); } catch (Exception e) { return false; } }
@Override public InputStream _newInputStream() throws IOException { ZipEntry e = zipFile.getEntry(path); return e != null ? zipFile.getInputStream(e) : null; } }
private static void unzipRepo() { try { ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip"); Enumeration<?> enu = zipFile.entries(); while (enu.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry) enu.nextElement(); String name = BUILD_PATH + "/" + zipEntry.getName(); File file = new File(name); if (name.endsWith("/")) { InputStream is = zipFile.getInputStream(zipEntry); FileOutputStream fos = new FileOutputStream(file); byte[] bytes = new byte[1024]; int length; while ((length = is.read(bytes)) >= 0) { fos.write(bytes, 0, length); is.close(); fos.close(); zipFile.close(); } catch (IOException e) { throw new RuntimeException(e);
OutputStream output = new FileOutputStream(StorezipFileLocation); while ((count = input.read(data)) != -1) input.close(); result = "true"; try ZipFile zipfile = new ZipFile(archive); for (Enumeration e = zipfile.entries(); e.hasMoreElements();) ZipEntry entry = (ZipEntry) e.nextElement(); unzipEntry(zipfile, entry, destinationPath); if (entry.isDirectory()) createDir(new File(outputDir, entry.getName())); return; File outputFile = new File(outputDir, entry.getName()); if (!outputFile.getParentFile().exists()) BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry)); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
public static void decompress(final File inputFile, final File destDir) throws IOException { try (ZipFile zipFile = new ZipFile(inputFile)) { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File newFile = getZipOutputFile(destDir, entry); if (entry.isDirectory()) { // Assume directories are stored parents first then children. newFile.mkdir(); continue; } InputStream stream = zipFile.getInputStream(entry); 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) { IOException ioe = new IOException("Not valid archive file type."); ioe.initCause(e); throw ioe; } finally { fos.flush(); fos.close(); stream.close(); } } } }
file.deleteOnExit(); ZipFile zipFile = new ZipFile(url.getFile()); ZipEntry entry = zipFile.getEntry(this.resourceName); if (entry == null) { if (this.resourceName.startsWith("/")) { entry = zipFile.getEntry(this.resourceName.replaceFirst("/", "")); if (entry == null) { throw new FileNotFoundException("Resource " + this.resourceName + " not found"); long size = entry.getSize(); InputStream stream = zipFile.getInputStream(entry); FileOutputStream outputStream = new FileOutputStream(file); byte[] array = new byte[1024]; int rd = 0; long bytesRead = 0; do { rd = stream.read(array); outputStream.write(array, 0, rd); bytesRead += rd; } while (bytesRead < size); outputStream.flush(); outputStream.close(); stream.close(); zipFile.close();
ZipFile zipFile = null; try { zipFile = new ZipFile(filePath); for (Enumeration entry = zipFile.entries(); entry.hasMoreElements(); ) { ZipEntry zipEntry = (ZipEntry) entry.nextElement(); if (zipEntry.isDirectory()) { if (zipEntry.getSize() > 0) { File file = FileUtil.getFileByPath(unzipPath + "/" + zipEntry.getName(), false); OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); InputStream is = zipFile.getInputStream(zipEntry); byte[] buffer = new byte[4096]; int len = 0; while ((len = is.read(buffer)) >= 0) { os.write(buffer, 0, len); zipFile.close(); } catch (Exception e) { e.printStackTrace(); if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { e.printStackTrace();
checkDirectory(filePath); ZipFile zipFile = new ZipFile(fileName); Enumeration enumeration = zipFile.entries(); try { while (enumeration.hasMoreElements()) { ZipEntry entry = (ZipEntry) enumeration.nextElement(); if (entry.isDirectory()) { new File(filePath, entry.getName()).mkdirs(); continue; BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); File file = new File(filePath + File.separator + entry.getName()); BufferedOutputStream bos = null; try { fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, TypedValue.BUFFER_SIZE); fos.write(buf, 0, len); bos.flush(); bos.close(); zipFile.close();
private void copyAllButClassesDexAndSigFiles(ZipFile source, ZipOutputStream destination) throws IOException { Enumeration<? extends ZipEntry> sourceEntries = source.entries(); while (sourceEntries.hasMoreElements()) { ZipEntry sourceEntry = sourceEntries.nextElement(); String sourceEntryName = sourceEntry.getName(); if (sourceEntryName.endsWith(".dex") || isSignatureFile(sourceEntryName)) { continue; } // separate ZipEntry avoids compression problems due to encodings ZipEntry destinationEntry = new ZipEntry(sourceEntryName); // use the same compression method as the original (certain files // are stored, not compressed) destinationEntry.setMethod(sourceEntry.getMethod()); // copy other necessary fields for STORE method destinationEntry.setSize(sourceEntry.getSize()); destinationEntry.setCrc(sourceEntry.getCrc()); // finally craft new entry destination.putNextEntry(destinationEntry); InputStream zipEntryInput = source.getInputStream(sourceEntry); byte[] buffer = new byte[2048]; int bytesRead = zipEntryInput.read(buffer); while (bytesRead > 0) { destination.write(buffer, 0, bytesRead); bytesRead = zipEntryInput.read(buffer); } zipEntryInput.close(); } }
File file = new File(destDir, name); files.add(file); if (entry.isDirectory()) { return createOrExistsDir(file); } else { OutputStream out = null; try { in = new BufferedInputStream(zip.getInputStream(entry)); out = new BufferedOutputStream(new FileOutputStream(file)); byte buffer[] = new byte[BUFFER_LEN]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); in.close();
/** Extracts the libraries from the jar file to given local path. */ private void extractInternalFiles() throws IOException { Enumeration entries = _h2oJar.entries(); while( entries.hasMoreElements() ) { ZipEntry e = (ZipEntry) entries.nextElement(); String name = e.getName(); if( e.isDirectory() ) continue; // mkdirs() will handle these if(! name.endsWith(".jar") ) continue; // extract the entry File out = internalFile(name); out.getParentFile().mkdirs(); try { FileOutputStream fos = new FileOutputStream(out); BufferedInputStream is = new BufferedInputStream (_h2oJar.getInputStream(e)); BufferedOutputStream os = new BufferedOutputStream(fos); int read; byte[] buffer = new byte[4096]; while( (read = is.read(buffer)) != -1 ) os.write(buffer,0,read); os.flush(); fos.getFD().sync(); // Force the output; throws SyncFailedException if full os.close(); is.close(); } catch( FileNotFoundException ex ) { // Expected FNF if 2 H2O instances are attempting to unpack in the same directory } catch( IOException ex ) { Log.die("Unable to extract file "+name+" because of "+ex+". Make sure that directory " + _parentDir + " contains at least 50MB of free space to unpack H2O libraries."); throw ex; // dead code } } }
source = domain.getCodeSource(); url = source.getLocation(); uri = url.toURI(); zipFile = new ZipFile(location); zipFile.close(); entry = zipFile.getEntry(fileName); throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName()); zipStream = zipFile.getInputStream(entry); fileStream = null; int i; fileStream = new FileOutputStream(tempFile); buf = new byte[1024]; i = 0; while((i = zipStream.read(buf)) != -1)
final String path = "sample/folder"; final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath()); if(jarFile.isFile()) { // Run with JAR file final JarFile jar = new JarFile(jarFile); final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar while(entries.hasMoreElements()) { final String name = entries.nextElement().getName(); if (name.startsWith(path + "/")) { //filter according to the path System.out.println(name); } } jar.close(); } else { // Run with IDE final URL url = Launcher.class.getResource("/" + path); if (url != null) { try { final File apps = new File(url.toURI()); for (File app : apps.listFiles()) { System.out.println(app); } } catch (URISyntaxException ex) { // never happens } } }
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException { if (entry.isDirectory()) { createDir(new File(outputDir, entry.getName())); return; } File outputFile = new File(outputDir, 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 { outputStream.close(); inputStream.close(); } }
is = new BufferedInputStream(zipFile.getInputStream(entryFile)); os = new BufferedOutputStream(new FileOutputStream(extractTo)); byte[] buffer = new byte[ShareConstants.BUFFER_SIZE]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length);
try { zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(extractTo))); bis = new BufferedInputStream(zipFile.getInputStream(entryFile)); ZipEntry entry = new ZipEntry(ShareConstants.DEX_IN_JAR); zos.putNextEntry(entry); int length = bis.read(buffer);