Java Code Examples for java.util.zip.ZipEntry#getMethod()

The following examples show how to use java.util.zip.ZipEntry#getMethod() . 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: 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 2
Source File: ApkUtils.java    From apk-shrink with Apache License 2.0 5 votes vote down vote up
public static List<String> unzipApk(String fileName, String filePath)
        throws IOException {
    FileUtils.checkDirectory(filePath);

    List<String> storedFiles = new ArrayList<>();
    ZipFile zipFile = new ZipFile(fileName);
    Enumeration em = zipFile.entries();
    while (em.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) em.nextElement();
        if (entry.isDirectory()) {
            new File(filePath, entry.getName()).mkdirs();
        } else {
            File file = new File(filePath + File.separator + entry.getName());
            File parent = file.getParentFile();
            if ((parent != null) && (!parent.exists())) {
                parent.mkdirs();
            }
            if (entry.getMethod() == 0) {
                storedFiles.add(entry.getName());
            }
            BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);

            byte[] buf = new byte[BUFFER];
            int len;
            while ((len = bis.read(buf, 0, 8192)) != -1) {
                fos.write(buf, 0, len);
            }
            bos.flush();
            bos.close();
            bis.close();
        }
    }
    zipFile.close();
    return storedFiles;
}
 
Example 3
Source File: Utils.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 4
Source File: Utils.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 5
Source File: CustomZipOutputStream.java    From buck with Apache License 2.0 5 votes vote down vote up
private void validateEntry(ZipEntry entry) {
  if (entry.getMethod() == ZipEntry.STORED) {
    Preconditions.checkState(
        entry.getCompressedSize() == entry.getSize(),
        "STORED entry where compressed != uncompressed size");
  }
}
 
Example 6
Source File: JarSigner.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void writeEntry(ZipFile zf, ZipOutputStream os, ZipEntry ze)
        throws IOException {
    ZipEntry ze2 = new ZipEntry(ze.getName());
    ze2.setMethod(ze.getMethod());
    ze2.setTime(ze.getTime());
    ze2.setComment(ze.getComment());
    ze2.setExtra(ze.getExtra());
    if (ze.getMethod() == ZipEntry.STORED) {
        ze2.setSize(ze.getSize());
        ze2.setCrc(ze.getCrc());
    }
    os.putNextEntry(ze2);
    writeBytes(zf, ze, os);
}
 
Example 7
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 8
Source File: ZippedEntry.java    From rxjava-extras with Apache License 2.0 5 votes vote down vote up
public ZippedEntry(ZipEntry e, InputStream is) {
    this.name = e.getName();
    this.time = e.getTime();
    // this.mtime = e.getLastModifiedTime();
    // this.atime = e.getLastAccessTime();
    // this.ctime = e.getCreationTime();
    this.crc = e.getCrc();
    this.size = e.getSize();
    this.csize = e.getCompressedSize();
    this.method = e.getMethod();
    this.extra = e.getExtra();
    this.comment = e.getComment();
    this.is = is;
}
 
Example 9
Source File: SignedJarBuilder.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the content of a Jar/Zip archive into the receiver archive.
 * <p/>An optional {@link IZipEntryFilter} allows to selectively choose which files
 * to copy over.
 *
 * @param input  the {@link InputStream} for the Jar/Zip to copy.
 * @param filter the filter or <code>null</code>
 * @throws IOException
 */
public void writeZip(InputStream input, IZipEntryFilter filter)
        throws IOException, IZipEntryFilter.ZipAbortException {
    ZipInputStream zis = new ZipInputStream(input);
    try {
        // loop on the entries of the intermediary package and put them in the final package.
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            String name = entry.getName();
            // do not take directories or anything inside a potential META-INF folder.
            if (entry.isDirectory() || name.startsWith("META-INF/")) {
                continue;
            }
            // if we have a filter, we check the entry against it
            if (filter != null && filter.checkEntry(name) == false) {
                continue;
            }
            JarEntry newEntry;
            // Preserve the STORED method of the input entry.
            if (entry.getMethod() == JarEntry.STORED) {
                newEntry = new JarEntry(entry);
            } else {
                // Create a new entry so that the compressed len is recomputed.
                newEntry = new JarEntry(name);
            }
            writeEntry(zis, newEntry);
            zis.closeEntry();
        }
    } finally {
        zis.close();
    }
}
 
Example 10
Source File: AutoSTOREDZipOutputStream.java    From dexdiff with Apache License 2.0 5 votes vote down vote up
@Override
public void putNextEntry(ZipEntry e) throws IOException {
    if (e.getMethod() != ZipEntry.STORED) {
        super.putNextEntry(e);
    } else {
        delayedEntry = e;
        if (delayedOutputStream == null) {
            delayedOutputStream = new AccessBufByteArrayOutputStream();
        }
    }
}
 
Example 11
Source File: LocalSignHelper.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static Predicate<String> getNoCompressPredicate(String srcPath) throws IOException {
    Set<String> noCompressEntries = new HashSet<>();
    ZipFile zipFile = new ZipFile(srcPath);
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()){
        ZipEntry zipEntry = entries.nextElement();
        if (zipEntry.getMethod() == 0){
            if (zipEntry.getName().endsWith(".so")){
                nativeLibrariesPackagingMode = NativeLibrariesPackagingMode.UNCOMPRESSED_AND_ALIGNED;
            }
            noCompressEntries.add(zipEntry.getName());
        }
    }
    return s -> noCompressEntries.contains(s);
}
 
Example 12
Source File: Utils.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 13
Source File: ZipBuilder.java    From bundletool with Apache License 2.0 5 votes vote down vote up
/**
 * Lazily copies the given zip file entry to the specified path, preserving the existing
 * compression setting.
 *
 * <p>Will throw an exception if the path is already taken.
 */
public ZipBuilder addFileFromZipPreservingCompression(
    ZipPath toPath, ZipFile fromZipFile, ZipEntry zipEntry) {
  EntryOption[] entryOption =
      zipEntry.getMethod() == ZipEntry.STORED
          ? new EntryOption[] {EntryOption.UNCOMPRESSED}
          : new EntryOption[0];
  return addFileFromZip(toPath, fromZipFile, zipEntry, entryOption);
}
 
Example 14
Source File: Utils.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 15
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 16
Source File: ZippedEntry.java    From rxjava2-extras with Apache License 2.0 5 votes vote down vote up
public ZippedEntry(ZipEntry e, InputStream is) {
    this.name = e.getName();
    this.time = e.getTime();
    // this.mtime = e.getLastModifiedTime();
    // this.atime = e.getLastAccessTime();
    // this.ctime = e.getCreationTime();
    this.crc = e.getCrc();
    this.size = e.getSize();
    this.csize = e.getCompressedSize();
    this.method = e.getMethod();
    this.extra = e.getExtra();
    this.comment = e.getComment();
    this.is = is;
}
 
Example 17
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 18
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static String zeString(ZipEntry ze) {
    int store = (ze.getCompressedSize() > 0) ?
        (int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 )
        : 0 ;
    // Follow unzip -lv output
    return ze.getSize() + "\t" + ze.getMethod()
        + "\t" + ze.getCompressedSize() + "\t"
        + store + "%\t"
        + new Date(ze.getTime()) + "\t"
        + Long.toHexString(ze.getCrc()) + "\t"
        + ze.getName() ;
}
 
Example 19
Source File: StrictJarFile.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private InputStream getZipInputStream(ZipEntry ze) {
    if (ze.getMethod() == ZipEntry.STORED) {
        return new FDStream(fd, ze.getDataOffset(),
                ze.getDataOffset() + ze.getSize());
    } else {
        final FDStream wrapped = new FDStream(
                fd, ze.getDataOffset(), ze.getDataOffset() + ze.getCompressedSize());

        int bufSize = Math.max(1024, (int) Math.min(ze.getSize(), 65535L));
        return new ZipInflaterInputStream(wrapped, new Inflater(true), bufSize, ze);
    }
}
 
Example 20
Source File: ZipFileEntryInputStream.java    From fingen with Apache License 2.0 4 votes vote down vote up
/**
 * position input stream to start of ZipEntry this instance was created for
 *
 * @throws IOException
 */
public void nextEntry( ZipEntry ze ) throws IOException {
	LOG.fine("nextEntry().currentPos=" + currentPos);
	
	byte[] intBuffer = new byte[4];
	int bytesRead = fis.read(intBuffer);
	LOG.fine("bytes read="+bytesRead);
	if( bytesRead==-1 ) {
		// this occurred on android once, with FileInputStream as my superclass
		throw new IOException("no data available - available=" + fis.available());
	}
	
	int dataDescriptorLength = 0;
	if( Arrays.equals(intBuffer, new byte[] { 0x50, 0x4b, 0x07, 0x08 }) ) {
		// header does not belong to next file, but is start of the "data descriptor" of last file
		// skip this data descriptor containing crc32(4), compressedSize(4), uncompressedSize(4)
		dataDescriptorLength = 4 + 4 + 4;
		fis.skip( dataDescriptorLength );
		// read local file header signature
		fis.read(intBuffer);
	}
	
	if( !Arrays.equals(intBuffer, new byte[] { 0x50, 0x4b, 0x03, 0x04 }) ) {
		throw new IOException("wrong local file header signature - value=" + ByteArrayHelper.toString(intBuffer) );
	}

	// info only - if bit-3 is set, current entry is followed by data descriptor
	boolean hasDataDescriptor = (ze.getMethod() & 8) > 0;
	LOG.fine( "nextEntry().hasDataDescriptor=" + hasDataDescriptor );

	this.compressedSize = ze.getCompressedSize();
	
	fis.skip(14 + 4 + 4); // 14 + localFileHeaderSignature(4) + compressedSize(4) + size(4)

	byte[] shortBuffer = new byte[2];
	fis.read(shortBuffer);
	int fileNameLength = ByteArrayHelper.toInt(shortBuffer);

	fis.read(shortBuffer);
	int extraFieldLength = ByteArrayHelper.toInt(shortBuffer);

	startPos = 18 + 12 + fileNameLength + extraFieldLength + dataDescriptorLength;
	currentPos = startPos;
	endPos = startPos + this.compressedSize;

	fis.skip( fileNameLength + extraFieldLength );
}