org.apache.commons.compress.archivers.zip.ZipArchiveInputStream类的使用及代码示例

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

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

ZipArchiveInputStream介绍

[英]Implements an input stream that can read Zip archives.

As of Apache Commons Compress it transparently supports Zip64 extensions and thus individual entries and archives larger than 4 GB or with more than 65536 entries.

The ZipFile class is preferred when reading from files as ZipArchiveInputStream is limited by not being able to read the central directory header before returning entries. In particular ZipArchiveInputStream

  • may return entries that are not part of the central directory at all and shouldn't be considered part of the archive.
  • may return several entries with the same name.
  • will not return internal or external attributes.
  • may return incomplete extra field data.
  • may return unknown sizes and CRC values for entries until the next entry has been reached if the archive uses the data descriptor feature.
    [中]实现一个可以读取Zip存档的输入流。
    从Apache Commons Compress开始,它透明地支持Zip64扩展,因此单个条目和存档大于4GB或超过65536个条目。
    当从文件中读取时,ZipFile类是首选的,因为ZipArchiveInputStream由于无法在返回条目之前读取中心目录头而受到限制。尤其是ZipArchiveInputStream
    *可能返回的条目根本不属于中央目录,也不应被视为存档的一部分。
    *可能会返回多个同名条目。
    *不会返回内部或外部属性。
    *可能会返回不完整的额外字段数据。
    *如果存档使用数据描述符功能,则在到达下一个条目之前,可能会返回未知的条目大小和CRC值。

代码示例

代码示例来源:origin: jeremylong/DependencyCheck

/**
 * Extracts the contents of an archive into the specified directory.
 *
 * @param archive an archive file such as a WAR or EAR
 * @param destination a directory to extract the contents to
 * @param filter determines which files get extracted
 * @throws ExtractionException thrown if the archive is not found
 */
public static void extractFilesUsingFilter(File archive, File destination, FilenameFilter filter) throws ExtractionException {
  if (archive == null || destination == null) {
    return;
  }
  try (FileInputStream fis = new FileInputStream(archive)) {
    extractArchive(new ZipArchiveInputStream(new BufferedInputStream(fis)), destination, filter);
  } catch (FileNotFoundException ex) {
    final String msg = String.format("Error extracting file `%s` with filter: %s", archive.getAbsolutePath(), ex.getMessage());
    LOGGER.debug(msg, ex);
    throw new ExtractionException(msg);
  } catch (IOException | ArchiveExtractionException ex) {
    LOGGER.warn("Exception extracting archive '{}'.", archive.getAbsolutePath());
    LOGGER.debug("", ex);
    throw new ExtractionException("Unable to extract from archive", ex);
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
FileOutputStream fos = new FileOutputStream(tmpFile);
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos);
while ((ze = zis.getNextZipEntry()) != null) {
  ZipArchiveEntry zeNew = new ZipArchiveEntry(ze.getName());
  zeNew.setComment(ze.getComment());
  zeNew.setExtra(ze.getExtra());
  zeNew.setTime(ze.getTime());
zis.close();

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

@Override
public void process(final InputStream in) throws IOException {
  int fragmentCount = 0;
  try (final ZipArchiveInputStream zipIn = new ZipArchiveInputStream(new BufferedInputStream(in))) {
    ArchiveEntry zipEntry;
    while ((zipEntry = zipIn.getNextEntry()) != null) {
      if (zipEntry.isDirectory() || !fileMatches(zipEntry)) {
        continue;

代码示例来源:origin: pxb1988/dex2jar

@Test
public void test1() throws IOException {
  ZipArchiveInputStream zis = new ZipArchiveInputStream(BadZipEntryFlagTest.class.getResourceAsStream("/bad.zip"));
  for (ZipArchiveEntry e = zis.getNextZipEntry(); e != null; e = zis.getNextZipEntry()) {
    e.getGeneralPurposeBit().useEncryption(false);
    if (!e.isDirectory()) {
      zis.read();
      System.out.println(e.getName());
    }
  }
}

代码示例来源:origin: plutext/docx4j

public ZipPartStore(InputStream is) throws Docx4JException {
  initMaxBytes();
  
  partByteArrays = new HashMap<String, ByteArray>();
  try {
    ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
    ArchiveEntry entry = null;
    while ((entry = zis.getNextEntry()) != null) {
      // How to read the data descriptor for length? ie before reading?
      byte[] bytes =  getBytesFromInputStream( zis );
      //log.debug("Extracting " + entry.getName());
      policePartSize(null, bytes.length, entry.getName()); 
      partByteArrays.put(entry.getName(), new ByteArray(bytes) );
    }
    zis.close();
  } catch (PartTooLargeException e) {
    throw e;
  } catch (Exception e) {
    throw new Docx4JException("Error processing zip file (is it a zip file?)", e);
  }
}

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

private static MediaType tryStreamingDetection(TikaInputStream stream) {
  Set<String> entryNames = new HashSet<>();
  try (InputStream is = new FileInputStream(stream.getFile())) {
    ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is);
    ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
    while (zae != null) {
      if (zae.isDirectory()) {
        zae = zipArchiveInputStream.getNextZipEntry();
        continue;
      entryNames.add(zae.getName());
      if (zae.getName().equals("[Content_Types].xml")) {
        MediaType mt = parseContentTypes(zipArchiveInputStream);
        if (mt != null) {
      zae = zipArchiveInputStream.getNextZipEntry();

代码示例来源:origin: org.xwiki.commons/xwiki-commons-filter-test

public static boolean isZip(File file) throws IOException
{
  final byte[] signature = new byte[12];
  int signatureLength;
  try (FileInputStream stream = new FileInputStream(file)) {
    stream.mark(signature.length);
    signatureLength = stream.read(signature);
  }
  return ZipArchiveInputStream.matches(signature, signatureLength);
}

代码示例来源:origin: edu.toronto.cs.medsavant/medsavant-shared

InputStream is = new BufferedInputStream(new FileInputStream(f));
List<File> files;
  files = unArchive(dest, (ArchiveInputStream)is);
} else if (lcfn.endsWith(".zip")) {
  is = new ZipArchiveInputStream(is);
  files = unArchive(dest, (ArchiveInputStream)is);
} else{
  files.add(outputFile);            
is.close();
if(f.exists()){
  f.delete();

代码示例来源:origin: pl.edu.icm.synat/synat-business-services-impl

private List<String> getDataSetXmlList(final InputStream inputStream) throws IOException {
  List<String> xmlList = new ArrayList<String>();
  if (inputStream == null) {
    return xmlList;
  }
  ZipArchiveInputStream zipInput = new ZipArchiveInputStream(inputStream);
  ZipArchiveEntry entry = null;
  try {
    while ((entry = zipInput.getNextZipEntry()) != null) {
      if (entry.isDirectory() || !entry.getName().endsWith("xml")) {
        continue;
      }
      byte[] buffer = new byte[(int) entry.getSize()];
      IOUtils.read(zipInput, buffer);
      String xml = new String(buffer);
      xmlList.add(xml);
    }
  } finally {
    zipInput.close();
  }
  return xmlList;
}

代码示例来源:origin: com.github.duanxinyuan/library-util-common

try (InputStream is = new FileInputStream(file);
   ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is)) {
  ArchiveEntry archiveEntry;
  while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {

代码示例来源:origin: org.apache.commons/commons-compress

if (!in.markSupported()) {
  throw new IllegalArgumentException("Mark is not supported.");
in.mark(signature.length);
int signatureLength = -1;
try {
  signatureLength = IOUtils.readFully(in, signature);
  in.reset();
} catch (IOException e) {
  throw new ArchiveException("IOException while reading signature.", e);
if (ZipArchiveInputStream.matches(signature, signatureLength)) {
  return ZIP;
} else if (JarArchiveInputStream.matches(signature, signatureLength)) {

代码示例来源:origin: de.flapdoodle.embedmongo/de.flapdoodle.embedmongo

progressListener.start(progressLabel);
FileInputStream fin = new FileInputStream(source);
BufferedInputStream in = new BufferedInputStream(fin);
ZipArchiveInputStream zipIn = new ZipArchiveInputStream(in);
try {
  ZipArchiveEntry entry;
  while ((entry = zipIn.getNextZipEntry()) != null) {
    if (file.matcher(entry.getName()).matches()) {
      if (zipIn.canReadEntryData(entry)) {
        long size = entry.getSize();
        Files.write(zipIn, size, destination);
        destination.setExecutable(true);
  zipIn.close();

代码示例来源:origin: org.apache.slider/slider-core

InputStream is = null;
try(FSDataInputStream appStream = fs.open(appPath)) {
 ZipArchiveInputStream zis = new ZipArchiveInputStream(appStream);
 ZipArchiveEntry zipEntry;
 boolean done = false;
 while (!done && (zipEntry = zis.getNextZipEntry()) != null) {
  if (entry.equals(zipEntry.getName())) {
   int size = (int) zipEntry.getSize();
   if (size != -1) {
    log.info("Reading {} of size {}", zipEntry.getName(),
      zipEntry.getSize());
    byte[] content = new byte[size];
    int offset = 0;
    while (offset < size) {
     offset += zis.read(content, offset, size - offset);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while (true) {
     int byteRead = zis.read();
     if (byteRead == -1) {
      break;

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

try {
  byte[] buffer = new byte[8192];
  fis = new FileInputStream(inputZip);
  zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names
  ArchiveEntry entry;
  while ((entry = zis.getNextEntry()) != null) {
    File file = new File(outputFolder, entry.getName());
    if (entry.isDirectory()) {
      fos = new FileOutputStream(file);
      int read;
      while ((read = zis.read(buffer,0,buffer.length)) != -1)
        fos.write(buffer,0,read);
      fos.close();
  try { zis.close(); } catch (Exception e) { }
  try { fis.close(); } catch (Exception e) { }
  try { if (fos!=null) fos.close(); } catch (Exception e) { }

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

private static void repairCopy(File brokenZip, File fixedZip) {
  try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(fixedZip)) {
    try (InputStream is = new FileInputStream(brokenZip)) {
      ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is);
      ZipArchiveEntry zae = zipArchiveInputStream.getNextZipEntry();
      while (zae != null) {
        try {
          if (!zae.isDirectory() && zipArchiveInputStream.canReadEntryData(zae)) {
            outputStream.putArchiveEntry(zae);
          zae = zipArchiveInputStream.getNextZipEntry();
        } catch (EOFException e) {
          break;

代码示例来源:origin: kriegaex/Galileo-Openbook-Cleaner

zipStream = new ZipArchiveInputStream(
  new BufferedInputStream(new FileInputStream(archiveFile), BUFFER_SIZE),
  "Cp437", false
);
while ((zipEntry = zipStream.getNextZipEntry()) != null) {
  SimpleLogger.debug("  Extracting " + zipEntry);
  unzippedFile = new File(targetDirectory, zipEntry.getName());
    throw new IOException("Cannot create directory '" + parentDir + "'");
  outUnzipped = new BufferedOutputStream(new FileOutputStream(unzippedFile), BUFFER_SIZE);
  while ((byteCount = zipStream.read(buffer, 0, BUFFER_SIZE)) != -1)
    outUnzipped.write(buffer, 0, byteCount);
  outUnzipped.close();
try {
  if (zipStream != null)
    zipStream.close();
} catch (IOException ignored) {}

代码示例来源:origin: com.atlassian.jira/jira-core

final ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new FileInputStream(zipFile));
ZipArchiveEntry entry = zipInputStream.getNextZipEntry();
  entry = zipInputStream.getNextZipEntry();
  currentEntry++;

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

ZipFile zipFile = new ZipFile("C:\\test.zip");
     byte[] buf = new byte[65536];
     Enumeration<?> entries = zipFile.getEntries();
     while (entries.hasMoreElements()) {
       ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) entries.nextElement();
       int n;
       InputStream is = zipFile.getInputStream(zipArchiveEntry);
       ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
       if (zis.canReadEntryData(zipArchiveEntry)) {
         while ((n = is.read(buf)) != -1) {
           if (n > 0) {
             System.out.println(new String(buf));
           }
         }
       }
       zis.close();
     }

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

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context)
    throws IOException, SAXException, TikaException {
  ZipArchiveInputStream zip = new ZipArchiveInputStream(stream);
  ZipArchiveEntry entry = zip.getNextZipEntry();
    if (!IWORK_CONTENT_ENTRIES.contains(entry.getName())) {
      entry = zip.getNextZipEntry();
      continue;
    entryStream.mark(4096);
    IWORKDocumentType type = IWORKDocumentType.detectType(entryStream);
    entryStream.reset();
    entry = zip.getNextZipEntry();

代码示例来源:origin: Alfresco/alfresco-repository

@Override
  public Set<String> execute() throws Throwable
  {
    Set<String> entryNames = new TreeSet<String>();
    ContentReader reader = CONTENT_SERVICE.getReader(downloadNode, ContentModel.PROP_CONTENT);
    ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(reader.getContentInputStream());
    try 
    {
      ZipArchiveEntry zipEntry = zipInputStream.getNextZipEntry();
      while (zipEntry != null)
      {
        String name = zipEntry.getName();
        entryNames.add(name);
        zipEntry = zipInputStream.getNextZipEntry();
      }
    }
    finally
    {
      zipInputStream.close();
    }
    return entryNames;
  }
});

相关文章