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

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

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

ZipFile.<init>介绍

[英]Constructs a new ZipFile allowing read access to the contents of the given file.
[中]构造一个新的ZipFile,允许对给定文件的内容进行读取访问。

代码示例

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

/**
 * @return null if jarFile doesn't contain a manifest, the
 * manifest otherwise.
 * @since Ant 1.5.2
 */
private Manifest getManifestFromJar(File jarFile) throws IOException {
  try (ZipFile zf = new ZipFile(jarFile)) {
    // must not use getEntry as "well behaving" applications
    // must accept the manifest in any capitalization
    ZipEntry ze = StreamUtils.enumerationAsStream(zf.entries())
        .filter(entry -> MANIFEST_NAME.equalsIgnoreCase(entry.getName()))
        .findFirst().orElse(null);
    if (ze == null) {
      return null;
    }
    try (InputStreamReader isr = new InputStreamReader(zf.getInputStream(ze), "UTF-8")) {
      return getManifest(isr);
    }
  }
}

代码示例来源: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: 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: 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: 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: stackoverflow.com

ZipFile zipFile = new ZipFile("file.zip");
ZipEntry zipEntry = zipFile.getEntry("fileName.txt");       
InputStream inputStream = zipFile.getInputStream(zipEntry);

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

private InputStream readFile (String path) {
  if (nativesJar == null) {
    InputStream input = SharedLibraryLoader.class.getResourceAsStream("/" + path);
    if (input == null) throw new GdxRuntimeException("Unable to read file for extraction: " + path);
    return input;
  }
  // Read from JAR.
  try {
    ZipFile file = new ZipFile(nativesJar);
    ZipEntry entry = file.getEntry(path);
    if (entry == null) throw new GdxRuntimeException("Couldn't find '" + path + "' in JAR: " + nativesJar);
    return file.getInputStream(entry);
  } catch (IOException ex) {
    throw new GdxRuntimeException("Error reading '" + path + "' in JAR: " + nativesJar, ex);
  }
}

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

private InputStream readFile (String path) {
  if (nativesJar == null) {
    InputStream input = SharedLibraryLoader.class.getResourceAsStream("/" + path);
    if (input == null) throw new GdxRuntimeException("Unable to read file for extraction: " + path);
    return input;
  }
  // Read from JAR.
  try {
    ZipFile file = new ZipFile(nativesJar);
    ZipEntry entry = file.getEntry(path);
    if (entry == null) throw new GdxRuntimeException("Couldn't find '" + path + "' in JAR: " + nativesJar);
    return file.getInputStream(entry);
  } catch (IOException ex) {
    throw new GdxRuntimeException("Error reading '" + path + "' in JAR: " + nativesJar, ex);
  }
}

代码示例来源: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);
}

相关文章