下面列出了java.util.zip.ZipEntry#DEFLATED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
* 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();
}
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;
}
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;
}
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();
}
}
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;
}
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);
}
}
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());
}
}
public ZipEntrySubject thatIsCompressed() {
if (actual.getMethod() != ZipEntry.DEFLATED) {
failWithActual(simpleFact("expected to be compressed in the zip file."));
}
return this;
}
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);
}
}
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;
}
public void setCompress(boolean compress) {
storageMethod = compress ? ZipEntry.DEFLATED : ZipEntry.STORED;
}
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;
}
/**
* 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;
}