Tabnine Logo
ZipFile
Code IndexAdd Tabnine to your IDE (free)

How to use
ZipFile
in
java.util.zip

Best Java code snippets using java.util.zip.ZipFile (Showing top 20 results out of 11,619)

Refine searchRefine arrow

  • ZipEntry
  • Enumeration
  • FileOutputStream
  • InputStream
  • URL
  • BufferedOutputStream
origin: skylot/jadx

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;
}
origin: Tencent/tinker

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.
      }
    }
  }
}
origin: Blankj/AndroidUtilCode

/**
 * 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;
}
origin: libgdx/libgdx

private InputStream getFromJar (String jarFile, String sharedLibrary) throws IOException {
  ZipFile file = new ZipFile(nativesJar);
  ZipEntry entry = file.getEntry(sharedLibrary);
  return file.getInputStream(entry);
}
origin: skylot/jadx

private static boolean isZipFileCanBeOpen(File file) {
  try (ZipFile zipFile = new ZipFile(file)) {
    return zipFile.entries().hasMoreElements();
  } catch (Exception e) {
    return false;
  }
}
origin: pxb1988/dex2jar

  @Override
  public InputStream _newInputStream() throws IOException {
    ZipEntry e = zipFile.getEntry(path);
    return e != null ? zipFile.getInputStream(e) : null;
  }
}
origin: twosigma/beakerx

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);
origin: stackoverflow.com

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));
origin: geoserver/geoserver

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();
      }
    }
  }
}
origin: deeplearning4j/nd4j

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();
origin: JZ-Darkal/AndroidHttpCapture

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();
origin: Tencent/tinker

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();
origin: Sable/soot

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();
 }
}
origin: Blankj/AndroidUtilCode

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();
origin: h2oai/h2o-2

/** 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
  }
 }
}
origin: stackoverflow.com

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)
origin: stackoverflow.com

 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
    }
  }
}
origin: marytts/marytts

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();
  }
}
origin: Tencent/tinker

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);
origin: Tencent/tinker

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);
java.util.zipZipFile

Javadoc

This class provides random read access to a zip file. You pay more to read the zip file's central directory up front (from the constructor), but if you're using #getEntry to look up multiple files by name, you get the benefit of this index.

If you only want to iterate through all the files (using #entries(), you should consider ZipInputStream, which provides stream-like read access to a zip file and has a lower up-front cost because you don't pay to build an in-memory index.

If you want to create a zip file, use ZipOutputStream. There is no API for updating an existing zip file.

Most used methods

  • <init>
  • getInputStream
    Returns an input stream on the data of the specified ZipEntry.
  • entries
  • close
    Closes this zip file. This method is idempotent. This method may cause I/O if the zip file needs to
  • getEntry
  • getName
    Gets the file name of this ZipFile.
  • size
    Returns the number of ZipEntries in this ZipFile.
  • stream
  • checkNotClosed
  • readCentralDir
    Find the central directory and read the contents. The central directory can be followed by a variab
  • throwZipException
  • getComment
    Returns this file's comment, or null if it doesn't have one. See ZipOutputStream#setComment.
  • throwZipException,
  • getComment

Popular in Java

  • Making http post requests using okhttp
  • requestLocationUpdates (LocationManager)
  • runOnUiThread (Activity)
  • onRequestPermissionsResult (Fragment)
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • Enumeration (java.util)
    A legacy iteration interface.New code should use Iterator instead. Iterator replaces the enumeration
  • Callable (java.util.concurrent)
    A task that returns a result and may throw an exception. Implementors define a single method with no
  • Pattern (java.util.regex)
    Patterns are compiled regular expressions. In many cases, convenience methods such as String#matches
  • Base64 (org.apache.commons.codec.binary)
    Provides Base64 encoding and decoding as defined by RFC 2045.This class implements section 6.8. Base
  • Runner (org.openjdk.jmh.runner)
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now