Java Code Examples for java.util.zip.ZipEntry#DEFLATED

The following examples show how to use java.util.zip.ZipEntry#DEFLATED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Jar.java    From helidon-build-tools with Apache License 2.0 6 votes vote down vote up
private static JarEntry newJarEntry(Entry entry) {
    final JarEntry result = new JarEntry(entry.getName());
    if (result.getCreationTime() != null) {
        result.setCreationTime(entry.getCreationTime());
    }
    if (result.getLastModifiedTime() != null) {
        result.setLastModifiedTime(entry.getLastModifiedTime());
    }
    if (entry.getExtra() != null) {
        result.setExtra(entry.getExtra());
    }
    if (result.getComment() != null) {
        result.setComment(entry.getComment());
    }
    if (!entry.isDirectory()) {
        final int method = entry.getMethod();
        if (method == JarEntry.STORED || method == ZipEntry.DEFLATED) {
            result.setMethod(method);
        }
    }
    return result;
}
 
Example 2
Source File: JarResources.java    From binnavi with Apache License 2.0 6 votes vote down vote up
/**
 * Dumps a zip entry into a string.
 * 
 * @param ze a ZipEntry
 */
private String dumpZipEntry(final ZipEntry ze) {
  final StringBuffer sb = new StringBuffer();
  if (ze.isDirectory()) {
    sb.append("d ");
  } else {
    sb.append("f ");
  }

  if (ze.getMethod() == ZipEntry.STORED) {
    sb.append("stored   ");
  } else {
    sb.append("defalted ");
  }

  sb.append(ze.getName());
  sb.append("\t");
  sb.append("" + ze.getSize());
  if (ze.getMethod() == ZipEntry.DEFLATED) {
    sb.append("/" + ze.getCompressedSize());
  }

  return sb.toString();
}
 
Example 3
Source File: ZipFileJarEntryContainer.java    From buck with Apache License 2.0 6 votes vote down vote up
private static CustomZipEntry makeCustomEntry(ZipEntry entry) {
  CustomZipEntry wrappedEntry = new CustomZipEntry(entry);

  // For deflated entries, the act of re-"putting" this entry means we're re-compressing
  // the data that we've just uncompressed.  Due to various environmental issues (e.g. a
  // newer version of zlib, changed compression settings), we may end up with a different
  // compressed size.  This causes an issue in java's `java.util.zip.ZipOutputStream`
  // implementation, as it only updates the compressed size field if one of `crc`,
  // `compressedSize`, or `size` is -1.  When we copy the entry as-is, none of these are
  // -1, and we may end up with an incorrect compressed size, in which case, we'll get an
  // exception.  So, for deflated entries, reset the compressed size to -1 (as the
  // ZipEntry(String) would).
  // See https://github.com/spearce/buck/commit/8338c1c3d4a546f577eed0c9941d9f1c2ba0a1b7.
  if (wrappedEntry.getMethod() == ZipEntry.DEFLATED) {
    wrappedEntry.setCompressedSize(-1);
  }

  return wrappedEntry;
}
 
Example 4
Source File: JarFileEntries.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
public InputStream getInputStream(FileHeader entry, ResourceAccess access) throws IOException {
    if (entry == null) {
        return null;
    }
    InputStream inputStream = getEntryData(entry).getInputStream(access);
    if (entry.getMethod() == ZipEntry.DEFLATED) {
        inputStream = new ZipInflaterInputStream(inputStream, (int) entry.getSize());
    }
    return inputStream;
}
 
Example 5
Source File: FileOperation.java    From AndResGuard with Apache License 2.0 5 votes vote down vote up
private static void zipFile(
    File resFile, ZipOutputStream zipout, String rootpath, HashMap<String, Integer> compressData) throws IOException {
  rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
  if (resFile.isDirectory()) {
    File[] fileList = resFile.listFiles();
    for (File file : fileList) {
      zipFile(file, zipout, rootpath, compressData);
    }
  } else {
    final byte[] fileContents = readContents(resFile);
    //这里需要强转成linux格式,果然坑!!
    if (rootpath.contains("\\")) {
      rootpath = rootpath.replace("\\", "/");
    }
    if (!compressData.containsKey(rootpath)) {
      System.err.printf(String.format("do not have the compress data path =%s in resource.asrc\n", rootpath));
      //throw new IOException(String.format("do not have the compress data path=%s", rootpath));
      return;
    }
    int compressMethod = compressData.get(rootpath);
    ZipEntry entry = new ZipEntry(rootpath);

    if (compressMethod == ZipEntry.DEFLATED) {
      entry.setMethod(ZipEntry.DEFLATED);
    } else {
      entry.setMethod(ZipEntry.STORED);
      entry.setSize(fileContents.length);
      final CRC32 checksumCalculator = new CRC32();
      checksumCalculator.update(fileContents);
      entry.setCrc(checksumCalculator.getValue());
    }
    zipout.putNextEntry(entry);
    zipout.write(fileContents);
    zipout.flush();
    zipout.closeEntry();
  }
}
 
Example 6
Source File: ZipContentItem.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public ZipContentItem( final ZipRepository repository,
                       final ZipContentLocation parent,
                       final ZipEntry zipEntry,
                       final byte[] bytes ) {
  if ( repository == null ) {
    throw new NullPointerException();
  }
  if ( zipEntry == null ) {
    throw new NullPointerException();
  }
  if ( bytes == null ) {
    throw new NullPointerException();
  }
  if ( parent == null ) {
    throw new NullPointerException();
  }

  this.parent = parent;
  this.repository = repository;
  this.comment = zipEntry.getComment();
  this.name = RepositoryUtilities.buildName( this, "/" );
  this.entryName = IOUtils.getInstance().getFileName( name );
  this.size = zipEntry.getSize();
  this.crc32 = zipEntry.getCrc();
  this.time = zipEntry.getTime();
  this.rawData = bytes;
  final int method = zipEntry.getMethod();
  if ( method == ZipEntry.STORED || method == ZipEntry.DEFLATED ) {
    this.method = new Integer( method );
  } else {
    this.method = new Integer( ZipEntry.DEFLATED );
  }
  this.compression = Deflater.DEFAULT_COMPRESSION;
}
 
Example 7
Source File: EntryAccounting.java    From buck with Apache License 2.0 5 votes vote down vote up
public static Method detect(int fromZipMethod) {
  switch (fromZipMethod) {
    case -1:
      return DEFLATE;
    case ZipEntry.DEFLATED:
      return DEFLATE;
    case ZipEntry.STORED:
      return STORE;
    default:
      throw new IllegalArgumentException("Cannot determine zip method from: " + fromZipMethod);
  }
}
 
Example 8
Source File: JBZipEntry.java    From consulo with Apache License 2.0 5 votes vote down vote up
private InputStream getInputStream() throws IOException {
  long start = calcDataOffset();

  BoundedInputStream bis = new BoundedInputStream(start, getCompressedSize());
  switch (getMethod()) {
    case ZipEntry.STORED:
      return bis;
    case ZipEntry.DEFLATED:
      bis.addDummy();
      return new InflaterInputStream(bis, new Inflater(true));
    default:
      throw new ZipException("Found unsupported compression method " + getMethod());
  }
}
 
Example 9
Source File: ZipEntrySubject.java    From bundletool with Apache License 2.0 4 votes vote down vote up
public ZipEntrySubject thatIsCompressed() {
  if (actual.getMethod() != ZipEntry.DEFLATED) {
    failWithActual(simpleFact("expected to be compressed in the zip file."));
  }
  return this;
}
 
Example 10
Source File: BootFile.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
private void readJar()
  throws IOException
{
  Supplier<InputStream> factory = ()->new BootInputStream("top", _bootMap, 0, _bootSize);
  
  try (BootZipScanner scanner = new BootZipScanner("top", factory, _bootSize)) {
    if (! scanner.open()) {
      throw new IllegalStateException();
    }
    
    while (scanner.next()) {
      String name = scanner.name();
      int position = scanner.offset();
      int size = scanner.sizeCompressed();
      
      if (name.endsWith(".jar")) {
        if (scanner.method() != ZipEntry.STORED
              || size <= 0
              || size >= Integer.MAX_VALUE
              || size != scanner.size()) {
          throw new IllegalStateException("Entry isn't stored: " + name);
        }
          
        JarItem jarItem = new JarItem(name, position, 
                                      scanner.sizeCompressed(),
                                      scanner.size(),
                                      scanner.method() == ZipEntry.DEFLATED);
        
        _jarMap.put(name, jarItem);
      }
      
      if (name.equalsIgnoreCase("meta-inf/manifest.mf")) {
        _manifestJarItem = new JarItem(name, position,
                                       scanner.sizeCompressed(),
                                       scanner.size(),
                                       scanner.method() == ZipEntry.DEFLATED);
      }
    }
  } catch (Exception e) {
    e.printStackTrace();;
    log.log(Level.FINER, _bootPath + " " + e.toString(), e);
  }
}
 
Example 11
Source File: HttpZipLocator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private int readTableEntry(byte[] table, int offset) throws IOException{
    if (get32(table, offset) != ZipEntry.CENSIG){
        throw new IOException("Central directory error, expected 'PK12'");
    }

    int nameLen = get16(table, offset + ZipEntry.CENNAM);
    int extraLen = get16(table, offset + ZipEntry.CENEXT);
    int commentLen = get16(table, offset + ZipEntry.CENCOM);
    int newOffset = offset + ZipEntry.CENHDR + nameLen + extraLen + commentLen;

    int flags = get16(table, offset + ZipEntry.CENFLG);
    if ((flags & 1) == 1){
        // ignore this entry, it uses encryption
        return newOffset;
    }
        
    int method = get16(table, offset + ZipEntry.CENHOW);
    if (method != ZipEntry.DEFLATED && method != ZipEntry.STORED){
        // ignore this entry, it uses unknown compression method
        return newOffset;
    }

    String name = getUTF8String(table, offset + ZipEntry.CENHDR, nameLen);
    if (name.charAt(name.length()-1) == '/'){
        // ignore this entry, it is directory node
        // or it has no name (?)
        return newOffset;
    }

    ZipEntry2 entry = new ZipEntry2();
    entry.name     = name;
    entry.deflate  = (method == ZipEntry.DEFLATED);
    entry.crc      = getu32(table, offset + ZipEntry.CENCRC);
    entry.length   = get32(table, offset + ZipEntry.CENLEN);
    entry.compSize = get32(table, offset + ZipEntry.CENSIZ);
    entry.offset   = get32(table, offset + ZipEntry.CENOFF);

    // we want offset directly into file data ..
    // move the offset forward to skip the LOC header
    entry.offset += ZipEntry.LOCHDR + nameLen + extraLen;

    entries.put(entry.name, entry);
    
    return newOffset;
}
 
Example 12
Source File: AndroidResourceOutputs.java    From bazel with Apache License 2.0 4 votes vote down vote up
public void setCompress(boolean compress) {
  storageMethod = compress ? ZipEntry.DEFLATED : ZipEntry.STORED;
}
 
Example 13
Source File: HttpZipLocator.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private int readTableEntry(byte[] table, int offset) throws IOException{
    if (get32(table, offset) != ZipEntry.CENSIG){
        throw new IOException("Central directory error, expected 'PK12'");
    }

    int nameLen = get16(table, offset + ZipEntry.CENNAM);
    int extraLen = get16(table, offset + ZipEntry.CENEXT);
    int commentLen = get16(table, offset + ZipEntry.CENCOM);
    int newOffset = offset + ZipEntry.CENHDR + nameLen + extraLen + commentLen;

    int flags = get16(table, offset + ZipEntry.CENFLG);
    if ((flags & 1) == 1){
        // ignore this entry, it uses encryption
        return newOffset;
    }
        
    int method = get16(table, offset + ZipEntry.CENHOW);
    if (method != ZipEntry.DEFLATED && method != ZipEntry.STORED){
        // ignore this entry, it uses unknown compression method
        return newOffset;
    }

    String name = getUTF8String(table, offset + ZipEntry.CENHDR, nameLen);
    if (name.charAt(name.length()-1) == '/'){
        // ignore this entry, it is directory node
        // or it has no name (?)
        return newOffset;
    }

    ZipEntry2 entry = new ZipEntry2();
    entry.name     = name;
    entry.deflate  = (method == ZipEntry.DEFLATED);
    entry.crc      = getu32(table, offset + ZipEntry.CENCRC);
    entry.length   = get32(table, offset + ZipEntry.CENLEN);
    entry.compSize = get32(table, offset + ZipEntry.CENSIZ);
    entry.offset   = get32(table, offset + ZipEntry.CENOFF);

    // we want offset directly into file data ..
    // move the offset forward to skip the LOC header
    entry.offset += ZipEntry.LOCHDR + nameLen + extraLen;

    entries.put(entry.name, entry);
    
    return newOffset;
}
 
Example 14
Source File: JBZipEntry.java    From consulo with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the compression method for the entry.
 *
 * @param method the compression method, either STORED or DEFLATED
 * @throws IllegalArgumentException if the specified compression
 *                                  method is invalid
 * @see #getMethod()
 */
public void setMethod(int method) {
  if (method != ZipEntry.STORED && method != ZipEntry.DEFLATED) {
    throw new IllegalArgumentException("invalid compression method");
  }
  this.method = method;
}