下面列出了java.util.zip.ZipEntry#getCompressedSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* java.util.zip.ZipOutputStream#setMethod(int)
*/
public void test_setMethodI() throws IOException {
ZipEntry ze = new ZipEntry("test");
zos.setMethod(ZipOutputStream.STORED);
CRC32 tempCrc = new CRC32();
tempCrc.update(data.getBytes());
ze.setCrc(tempCrc.getValue());
ze.setSize(new String(data).length());
zos.putNextEntry(ze);
zos.write(data.getBytes());
zos.closeEntry();
long csize = ze.getCompressedSize();
zos.setMethod(ZipOutputStream.DEFLATED);
zos.putNextEntry(ze = new ZipEntry("test2"));
zos.write(data.getBytes());
zos.closeEntry();
assertTrue("setLevel failed", csize >= ze.getCompressedSize());
}
public static boolean isZipBomb(ZipEntry entry) {
long compressedSize = entry.getCompressedSize();
long uncompressedSize = entry.getSize();
if (compressedSize < 0 || uncompressedSize < 0) {
LOG.error("Zip bomb attack detected, invalid sizes: compressed {}, uncompressed {}, name {}",
compressedSize, uncompressedSize, entry.getName());
return true;
}
if (compressedSize * MAX_SIZE_DIFF < uncompressedSize) {
LOG.error("Zip bomb attack detected, invalid sizes: compressed {}, uncompressed {}, name {}",
compressedSize, uncompressedSize, entry.getName());
return true;
}
return false;
}
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;
}
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() ;
}
@Override
public Void call() throws InterruptedException, IOException {
try {
Enumeration<? extends ZipEntry> entries = in.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// Since these entries come from an existing zip file, they should always know their size
// (meaning, never return -1). We also can't handle files that don't fit into a byte array.
checkArgument(entry.getSize() >= 0, "Cannot process entry with unknown size: %s", entry);
checkArgument(entry.getSize() <= Integer.MAX_VALUE, "Entry too large: %s", entry);
byte[] content;
if (entry.getSize() == 0L) {
content = EMPTY; // this in particular covers directory entries
} else {
try (InputStream entryStream = in.getInputStream(entry)) {
// Read all the content at once, which avoids temporary arrays and extra array copies
content = new byte[(int) entry.getSize()];
ByteStreams.readFully(entryStream, content); // throws if file is smaller than expected
checkState(entryStream.read() == -1,
"Too many bytes in jar entry %s, expected %s", entry, entry.getSize());
}
}
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
files.put(toDex(entry, content));
} else {
// Copy other files and directory entries
if (entry.getCompressedSize() != 0) {
entry.setCompressedSize(-1L); // We may compress differently from source Zip
}
files.put(immediateFuture(new ZipEntryContent(entry, content)));
}
}
} finally {
// Use try-finally to make absolutely sure we do this, otherwise the reader might deadlock
files.put(immediateFuture((ZipEntryContent) null)); // "end of stream" marker
}
return null;
}
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() ;
}
private static long compress(byte[] data) throws IOException {
ZipEntry zipEntry = new ZipEntry("entry");
try (ZipOutputStream zos = new ZipOutputStream(ByteStreams.nullOutputStream())) {
zipEntry.setMethod(ZipEntry.DEFLATED);
zos.putNextEntry(zipEntry);
zos.write(data);
zos.closeEntry();
}
return zipEntry.getCompressedSize();
}
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() ;
}
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() ;
}
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() ;
}
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() ;
}
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() ;
}
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() ;
}
public static boolean isZipBomb(ZipEntry entry) {
long compressedSize = entry.getCompressedSize();
long uncompressedSize = entry.getSize();
if (compressedSize < 0 || uncompressedSize < 0) {
LOG.error("Zip bomb attack detected, invalid sizes: compressed {}, uncompressed {}, name {}",
compressedSize, uncompressedSize, entry.getName());
return true;
}
if (compressedSize * MAX_SIZE_DIFF < uncompressedSize) {
LOG.error("Zip bomb attack detected, invalid sizes: compressed {}, uncompressed {}, name {}",
compressedSize, uncompressedSize, entry.getName());
return true;
}
return false;
}
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() ;
}
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() ;
}
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() ;
}
protected void fillFromZipInt(Uri uri, IProgressor progressor) throws IOException, NGException, RuntimeException {
InputStream inputStream;
String url = uri.toString();
if (NetworkUtil.isValidUri(url))
inputStream = new URL(url).openStream();
else
inputStream = mContext.getContentResolver().openInputStream(uri);
if (inputStream == null)
throw new NGException(mContext.getString(R.string.error_download_data));
int streamSize = inputStream.available();
if (null != progressor)
progressor.setMax(streamSize);
int increment = 0;
byte[] buffer = new byte[Constants.IO_BUFFER_SIZE];
ZipInputStream zis = new ZipInputStream(inputStream);
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
FileUtil.unzipEntry(zis, ze, buffer, mPath);
increment += ze.getCompressedSize();
zis.closeEntry();
if (null != progressor) {
if(progressor.isCanceled())
return;
progressor.setValue(increment);
progressor.setMessage(getContext().getString(R.string.processed) + " " + increment + " " + getContext().getString(R.string.of) + " " + streamSize);
}
}
}
@Override
public long getCompressedSize(String fileName)
throws DirectoryException {
ZipEntry entry = getZipFileEntry(fileName);
return entry.getCompressedSize();
}
/**
* 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 );
}