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

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

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

ZipFile.entries介绍

[英]Returns an enumeration of the entries. The entries are listed in the order in which they appear in the zip file.

If you only need to iterate over the entries in a zip file, and don't need random-access entry lookup by name, you should probably use ZipInputStreaminstead, to avoid paying to construct the in-memory index.
[中]返回项的枚举。这些条目按它们在zip文件中出现的顺序列出。
如果只需要迭代zip文件中的条目,而不需要按名称进行随机访问条目查找,那么可能应该使用ZipInputStream,以避免花钱构建内存索引。

代码示例

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

/**
 * Determines if a zip archive contains a particular directory.
 *
 * @param zipfile path to the zipped file
 * @param target  directory being looked for in the zip.
 * @return boolean whether or not the directory exists in the zip.
 */
public static boolean zipDoesContainDir(String zipfile, String target) throws IOException {
  List<ZipEntry> entries = (List<ZipEntry>) Collections.list(new ZipFile(zipfile).entries());
  String targetDir = target + "/";
  for (ZipEntry entry : entries) {
    String name = entry.getName();
    if (name.startsWith(targetDir)) {
      return true;
    }
  }
  return false;
}

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

private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException {
  // First, copy the contents from the existing outFile:
  Enumeration<? extends ZipEntry> entries = inputFile.entries();
  while (entries.hasMoreElements()) {
    ZipEntry entry = new ZipEntry(entries.nextElement());
    // We can't reuse the compressed size because it depends on compression sizes.
    entry.setCompressedSize(-1);
    outputFile.putNextEntry(entry);
    // No need to create directory entries in the final apk
    if (! entry.isDirectory()) {
      BrutIO.copy(inputFile, outputFile, entry);
    }
    outputFile.closeEntry();
  }
}

代码示例来源:origin: f2prateek/dart

private List<String> getJarContent(File file) {
  final List<String> result = new ArrayList<>();
  try {
   if (file.getName().endsWith(".jar")) {
    ZipFile zip = new ZipFile(file);
    Collections.list(zip.entries()).stream().map(ZipEntry::getName).forEach(result::add);
   }
  } catch (IOException e) {
   throw new RuntimeException(e);
  }
  return result;
 }
}

代码示例来源: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: org.apache.ant/ant

private boolean jarHasIndex(File jarFile) throws IOException {
  try (ZipFile zf = new ZipFile(jarFile)) {
    return StreamUtils.enumerationAsStream(zf.entries())
        .anyMatch(ze -> INDEX_NAME.equalsIgnoreCase(ze.getName()));
  }
}

代码示例来源:origin: cucumber/cucumber-jvm

ZipResourceIterator(String zipPath, String path, String suffix) throws IOException {
  this.path = path;
  this.suffix = suffix;
  jarFile = new ZipFile(zipPath);
  entries = jarFile.entries();
  moveToNext();
}

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

public static void main(String[] args) throws IOException {
  ZipFile zipFile = new ZipFile("C:/test.zip");

  Enumeration<? extends ZipEntry> entries = zipFile.entries();

  while(entries.hasMoreElements()){
    ZipEntry entry = entries.nextElement();
    InputStream stream = zipFile.getInputStream(entry);
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Test
public void zip_directory() throws IOException {
 File foo = FileUtils.toFile(getClass().getResource("/org/sonar/api/utils/ZipUtilsTest/shouldZipDirectory/foo.txt"));
 File dir = foo.getParentFile();
 File zip = temp.newFile();
 ZipUtils.zipDir(dir, zip);
 assertThat(zip).exists().isFile();
 assertThat(zip.length()).isGreaterThan(1L);
 Iterator<? extends ZipEntry> zipEntries = Iterators.forEnumeration(new ZipFile(zip).entries());
 assertThat(zipEntries).hasSize(4);
 File unzipDir = temp.newFolder();
 ZipUtils.unzip(zip, unzipDir);
 assertThat(new File(unzipDir, "bar.txt")).exists().isFile();
 assertThat(new File(unzipDir, "foo.txt")).exists().isFile();
 assertThat(new File(unzipDir, "dir1/hello.properties")).exists().isFile();
}

代码示例来源: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 List<String> getFiles(File jarFile)
  throws ZipException, IOException
 {
  //System.out.println("Looking at " + jarFile);
  List<String> files = new ArrayList<>();

  ZipFile zin = new ZipFile(jarFile);
  Enumeration<? extends ZipEntry> entries = zin.entries();
  while (entries.hasMoreElements()) {
   ZipEntry entry = entries.nextElement();
   String name = entry.getName();
   if (name.matches(pattern)) {
    files.add(name);
   }
  }
  Collections.sort(files);
  return files;
 }
}

代码示例来源:origin: oblac/jodd

/**
 * Lists zip content.
 */
public static List<String> listZip(final File zipFile) throws IOException {
  List<String> entries = new ArrayList<>();
  ZipFile zip = new ZipFile(zipFile);
  Enumeration zipEntries = zip.entries();
  while (zipEntries.hasMoreElements()) {
    ZipEntry entry = (ZipEntry) zipEntries.nextElement();
    String entryName = entry.getName();
    entries.add(entryName);
  }
  return Collections.unmodifiableList(entries);
}

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

// ITS PSEUDOCODE!!

private InputStream extractOnlyFile(String path) {
  ZipFile zf = new ZipFile(path);
  Enumeration e = zf.entries();
  ZipEntry entry = (ZipEntry) e.nextElement(); // your only file
  return zf.getInputStream(entry);
}

代码示例来源:origin: konsoletyper/teavm

@Override
public void open() throws IOException {
  zipFile = new ZipFile(file);
  for (Enumeration<? extends ZipEntry> enumeration = zipFile.entries(); enumeration.hasMoreElements();) {
    ZipEntry entry = enumeration.nextElement();
    sourceFiles.add(entry.getName());
  }
}

代码示例来源:origin: alibaba/jstorm

/**
 * Check whether the zipfile contains the resources
 */
public static boolean zipContainsDir(String zipfile, String resources) {
  Enumeration<? extends ZipEntry> entries;
  try {
    entries = (new ZipFile(zipfile)).entries();
    while (entries != null && entries.hasMoreElements()) {
      ZipEntry ze = entries.nextElement();
      String name = ze.getName();
      if (name.startsWith(resources + "/")) {
        return true;
      }
    }
  } catch (IOException e) {
    LOG.error("zipContainsDir error", e);
  }
  return false;
}

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

public static void unzip(File zipfile, File directory) throws IOException {
 ZipFile zfile = new ZipFile(zipfile);
 Enumeration<? extends ZipEntry> entries = zfile.entries();
 while (entries.hasMoreElements()) {
  ZipEntry entry = entries.nextElement();
  File file = new File(directory, entry.getName());
  if (entry.isDirectory()) {
   file.mkdirs();
  } else {
   file.getParentFile().mkdirs();
   InputStream in = zfile.getInputStream(entry);
   try {
    copy(in, file);
   } finally {
    in.close();
   }
  }
 }
}

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

try (
   java.util.zip.ZipFile zf = new java.util.zip.ZipFile(zipFileName);
   java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
 ) {
   for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {
     String newLine = System.getProperty("line.separator");
     String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;
     writer.write(zipEntryName, 0, zipEntryName.length());
   }
 }

代码示例来源:origin: EngineHub/WorldEdit

@Override
  public boolean isValid() {
    for (Enumeration<? extends ZipEntry> e = zip.entries(); e.hasMoreElements(); ) {

      ZipEntry testEntry = e.nextElement();

      if (testEntry.getName().matches(".*\\.mcr$") || testEntry.getName().matches(".*\\.mca$")) { // TODO: does this need a separate class?
        return true;
      }
    }

    return false;
  }
}

代码示例来源:origin: alibaba/jstorm

/**
 * Check whether the zipfile contain the resources
 *
 * @param zipfile
 * @param resources
 * @return
 */
public static boolean zipContainsDir(String zipfile, String resources) {
  Enumeration<? extends ZipEntry> entries = null;
  try {
    entries = (new ZipFile(zipfile)).entries();
    while (entries != null && entries.hasMoreElements()) {
      ZipEntry ze = entries.nextElement();
      String name = ze.getName();
      if (name.startsWith(resources + "/")) {
        return true;
      }
    }
  } catch (IOException e) {
    // TODO Auto-generated catch block
    // e.printStackTrace();
    LOG.error(e + "zipContainsDir error");
  }
  return false;
}

相关文章