java.util.zip.ZipFile.close()方法的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(9.1k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中java.util.zip.ZipFile.close()方法的一些代码示例,展示了ZipFile.close()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipFile.close()方法的具体详情如下:
包路径:java.util.zip.ZipFile
类名称:ZipFile
方法名:close

ZipFile.close介绍

[英]Closes this zip file. This method is idempotent. This method may cause I/O if the zip file needs to be deleted.
[中]关闭此zip文件。这个方法是幂等的。如果需要删除zip文件,此方法可能会导致I/O。

代码示例

代码示例来源:origin: Blankj/AndroidUtilCode

/**
 * Return the files' path in ZIP file.
 *
 * @param zipFile The ZIP file.
 * @return the files' path in ZIP file
 * @throws IOException if an I/O error has occurred
 */
public static List<String> getFilesPath(final File zipFile)
    throws IOException {
  if (zipFile == null) return null;
  List<String> paths = new ArrayList<>();
  ZipFile zip = new ZipFile(zipFile);
  Enumeration<?> entries = zip.entries();
  while (entries.hasMoreElements()) {
    String entryName = ((ZipEntry) entries.nextElement()).getName();
    if (entryName.contains("../")) {
      System.out.println("entryName: " + entryName + " is dangerous!");
      paths.add(entryName);
    } else {
      paths.add(entryName);
    }
  }
  zip.close();
  return paths;
}

代码示例来源:origin: spotbugs/spotbugs

public static void testZip(String filename, boolean b) throws IOException {
  ZipFile zip = new ZipFile(filename);
  if (b)
    zip.close();
}

代码示例来源:origin: Tencent/tinker

public static String getZipEntryMd5(File file, String entryName) {
  ZipFile zipFile = null;
  try {
    zipFile = new ZipFile(file);
    ZipEntry entry = zipFile.getEntry(entryName);
    if (entry == null) {
      return null;
    }
    return MD5.getMD5(zipFile.getInputStream(entry), 1024 * 100);
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (zipFile != null) {
      try {
        zipFile.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  return null;
}

代码示例来源:origin: Atmosphere/atmosphere

public InputStream next() throws IOException {
  while (entries.hasMoreElements()) {
    current = entries.nextElement();
    if (accept(current)) {
      return zipFile.getInputStream(current);
    }
  }
  // no more entries in this ZipFile, so close ZipFile
  try {
    // zipFile is never null here
    zipFile.close();
  } catch (IOException ex) { // SUPPRESS CHECKSTYLE EmptyBlockCheck
    // suppress IOException, otherwise close() is called twice
  }
  return null;
}

代码示例来源:origin: apache/ignite

zip = new ZipFile(zipFile);
for (ZipEntry entry : asIterable(zip.entries())) {
  if (entry.isDirectory()) {
    new File(toDir, entry.getName()).mkdirs();
    in = zip.getInputStream(entry);
    File outFile = new File(toDir, entry.getName());
  zip.close();

代码示例来源: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: stanfordnlp/CoreNLP

public static LexicalizedParser loadModelFromZip(String zipFilename,
                         String modelName) {
 LexicalizedParser parser = null;
 try {
  File file = new File(zipFilename);
  if (file.exists()) {
   ZipFile zin = new ZipFile(file);
   ZipEntry zentry = zin.getEntry(modelName);
   if (zentry != null) {
    InputStream in = zin.getInputStream(zentry);
    // gunzip it if necessary
    if (modelName.endsWith(".gz")) {
     in = new GZIPInputStream(in);
    }
    ObjectInputStream ois = new ObjectInputStream(in);
    parser = loadModel(ois);
    ois.close();
    in.close();
   }
   zin.close();
  } else {
   throw new FileNotFoundException("Could not find " + modelName +
                   " inside " + zipFilename);
  }
 } catch (IOException e) {
  throw new RuntimeIOException(e);
 }
 return parser;
}

代码示例来源:origin: ankidroid/Anki-Android

List<Object[]> media = new ArrayList<>();
  JSONObject meta = new JSONObject(Utils.convertStreamToString(z.getInputStream(z.getEntry("_meta"))));
  for (ZipEntry i : Collections.list(z.entries())) {
    if (i.getName().equals("_meta")) {
      String name = meta.getString(i.getName());
      Utils.writeToFile(z.getInputStream(i), destPath);
      String csum = Utils.fileChecksum(destPath);
  throw new RuntimeException(e);
} finally {
  z.close();

代码示例来源:origin: stackoverflow.com

static boolean isValid(final File file) {
  ZipFile zipfile = null;
  try {
    zipfile = new ZipFile(file);
    return true;
  } catch (IOException e) {
    return false;
  } finally {
    try {
      if (zipfile != null) {
        zipfile.close();
        zipfile = null;
      }
    } catch (IOException e) {
    }
  }
}

代码示例来源: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());
    zipFile.close();

代码示例来源:origin: Tencent/tinker

ZipFile zipFile = null;
try {
  zipFile = new ZipFile(file);
  ZipEntry entry = zipFile.getEntry(DexFormat.DEX_IN_JAR_NAME);
  if (entry != null) {
    InputStream inputStream = null;
    try {
      inputStream = zipFile.getInputStream(entry);
      loadFrom(inputStream, (int) entry.getSize());
    } finally {
  if (zipFile != null) {
    try {
      zipFile.close();
    } catch (Exception e) {

代码示例来源:origin: sixt/ja-micro

private List<RpcMethodDefinition> searchJar(String jarpath, String serviceName) throws IOException {
  List<RpcMethodDefinition> defs = new ArrayList<>();
  File jarFile = new File(jarpath);
  if (!jarFile.exists() || jarFile.isDirectory()) {
    return defs;
  }
  ZipInputStream zip = new ZipInputStream(new FileInputStream(jarFile));
  ZipEntry ze;
  while ((ze = zip.getNextEntry()) != null) {
    String entryName = ze.getName();
    if (entryName.endsWith(".proto")) {
      ZipFile zipFile = new ZipFile(jarFile);
      try (InputStream in = zipFile.getInputStream(ze)) {
        defs.addAll(inspectProtoFile(entryName, in, serviceName));
      }
      zipFile.close();
    }
  }
  zip.close();
  return defs;
}

代码示例来源:origin: stackoverflow.com

try{
  ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), 0);
  ZipFile zf = new ZipFile(ai.sourceDir);
  ZipEntry ze = zf.getEntry("classes.dex");
  long time = ze.getTime();
  String s = SimpleDateFormat.getInstance().format(new java.util.Date(time));
  zf.close();
}catch(Exception e){
}

代码示例来源:origin: stackoverflow.com

java.util.zip.ZipFile zipFile = new ZipFile(file);
try {
 Enumeration<? extends ZipEntry> entries = zipFile.entries();
 while (entries.hasMoreElements()) {
  ZipEntry entry = entries.nextElement();
  File entryDestination = new File(outputDir,  entry.getName());
  if (entry.isDirectory()) {
    entryDestination.mkdirs();
  } else {
    entryDestination.getParentFile().mkdirs();
    InputStream in = zipFile.getInputStream(entry);
    OutputStream out = new FileOutputStream(entryDestination);
    IOUtils.copy(in, out);
    IOUtils.closeQuietly(in);
    out.close();
  }
 }
} finally {
 zipFile.close();
}

代码示例来源:origin: iBotPeaches/Apktool

ZipOutputStream out = null;
try {
  ZipFile zip = new ZipFile(frameFile);
  ZipEntry entry = zip.getEntry("resources.arsc");
  in = zip.getInputStream(entry);
  byte[] data = IOUtils.toByteArray(in);
    in = zip.getInputStream(entry);
    byte[] manifest = IOUtils.toByteArray(in);
    CRC32 manifestCrc = new CRC32();
  zip.close();
  LOGGER.info("Framework installed to: " + outFile);
} catch (IOException ex) {

代码示例来源:origin: Tencent/tinker

public static String getZipEntryCrc(File file, String entryName) {
  ZipFile zipFile = null;
  try {
    zipFile = new ZipFile(file);
    ZipEntry entry = zipFile.getEntry(entryName);
    if (entry == null) {
      return null;
    }
    return String.valueOf(entry.getCrc());
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (zipFile != null) {
      try {
        zipFile.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  return null;
}

代码示例来源:origin: Sable/soot

@Override
 public Set<String> load(String archivePath) throws Exception {
  ZipFile archive = null;
  try {
   archive = new ZipFile(archivePath);
   Set<String> ret = new HashSet<String>();
   Enumeration<? extends ZipEntry> it = archive.entries();
   while (it.hasMoreElements()) {
    ret.add(it.nextElement().getName());
   }
   return ret;
  } finally {
   if (archive != null) {
    archive.close();
   }
  }
 }
});

代码示例来源:origin: deeplearning4j/nd4j

file.deleteOnExit();
ZipFile zipFile = new ZipFile(url.getFile());
ZipEntry entry = zipFile.getEntry(this.resourceName);
if (entry == null) {
InputStream stream = zipFile.getInputStream(entry);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] array = new byte[1024];
zipFile.close();

代码示例来源:origin: Tencent/tinker

private void getCompressMethodFromApk() {
  ZipFile zipFile = null;
  try {
    zipFile = new ZipFile(config.mNewApkFile);
    ArrayList<String> sets = new ArrayList<>();
    sets.addAll(modifiedSet);
    sets.addAll(addedSet);
    ZipEntry zipEntry;
    for (String name : sets) {
      zipEntry = zipFile.getEntry(name);
      if (zipEntry != null && zipEntry.getMethod() == ZipEntry.STORED) {
        storedSet.add(name);
      }
    }
  } catch (Throwable throwable) {
  } finally {
    if (zipFile != null) {
      try {
        zipFile.close();
      } catch (IOException e) {
      }
    }
  }
}

代码示例来源:origin: apache/hbase

/**
 * Add entries to <code>packagedClasses</code> corresponding to class files
 * contained in <code>jar</code>.
 * @param jar The jar who's content to list.
 * @param packagedClasses map[class -> jar]
 */
private static void updateMap(String jar, Map<String, String> packagedClasses) throws IOException {
 if (null == jar || jar.isEmpty()) {
  return;
 }
 ZipFile zip = null;
 try {
  zip = new ZipFile(jar);
  for (Enumeration<? extends ZipEntry> iter = zip.entries(); iter.hasMoreElements();) {
   ZipEntry entry = iter.nextElement();
   if (entry.getName().endsWith("class")) {
    packagedClasses.put(entry.getName(), jar);
   }
  }
 } finally {
  if (null != zip) zip.close();
 }
}

相关文章