下面列出了java.util.zip.ZipEntry#STORED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void writeEntries(JarFile jarFile, EntryTransformer entryTransformer) throws IOException {
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream(
jarFile.getInputStream(entry));
try {
if (inputStream.hasZipHeader() && entry.getMethod() != ZipEntry.STORED) {
new CrcAndSize(inputStream).setupStoredEntry(entry);
inputStream.close();
inputStream = new ZipHeaderPeekInputStream(jarFile.getInputStream(entry));
}
EntryWriter entryWriter = new InputStreamEntryWriter(inputStream, true);
JarEntry transformedEntry = entryTransformer.transform(entry);
if (transformedEntry != null) {
writeEntry(transformedEntry, entryWriter);
}
} finally {
inputStream.close();
}
}
}
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);
}
}
public static 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);
}
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files)
throws BrutException, IOException {
File unknownFileDir = new File(appDir, UNK_DIRNAME);
// loop through unknown files
for (Map.Entry<String,String> unknownFileInfo : files.entrySet()) {
File inputFile = new File(unknownFileDir, BrutIO.sanitizeUnknownFile(unknownFileDir, unknownFileInfo.getKey()));
if (inputFile.isDirectory()) {
continue;
}
ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
int method = Integer.parseInt(unknownFileInfo.getValue());
LOGGER.fine(String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method));
if (method == ZipEntry.STORED) {
newEntry.setMethod(ZipEntry.STORED);
newEntry.setSize(inputFile.length());
newEntry.setCompressedSize(-1);
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
CRC32 crc = BrutIO.calculateCrc(unknownFile);
newEntry.setCrc(crc.getValue());
} else {
newEntry.setMethod(ZipEntry.DEFLATED);
}
outputFile.putNextEntry(newEntry);
BrutIO.copy(inputFile, outputFile);
outputFile.closeEntry();
}
}
/**
* 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);
}
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);
}
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());
}
}
@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();
}
}
}
private static JarEntry smartClone(JarEntry originalJarEntry) {
final JarEntry newJarEntry = new JarEntry(originalJarEntry.getName());
newJarEntry.setComment(originalJarEntry.getComment());
newJarEntry.setExtra(originalJarEntry.getExtra());
newJarEntry.setMethod(originalJarEntry.getMethod());
newJarEntry.setTime(originalJarEntry.getTime());
//Must set size and CRC for STORED entries
if (newJarEntry.getMethod() == ZipEntry.STORED) {
newJarEntry.setSize(originalJarEntry.getSize());
newJarEntry.setCrc(originalJarEntry.getCrc());
}
return newJarEntry;
}
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;
}
private void validateEntry(ZipEntry entry) {
if (entry.getMethod() == ZipEntry.STORED) {
Preconditions.checkState(
entry.getCompressedSize() == entry.getSize(),
"STORED entry where compressed != uncompressed size");
}
}
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 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 ZipEntrySubject thatIsUncompressed() {
if (actual.getMethod() != ZipEntry.STORED) {
failWithActual(simpleFact("expected to be uncompressed in the zip file."));
}
return this;
}
public void obfuscate() throws IOException, BaffleException {
String tempDir = System.getProperty("java.io.tmpdir") + File.separator + "old" + File.separator;
log.log(Level.CONFIG, "tempDir:::" + tempDir);
File temp = new File(tempDir);
OS.rmdir(temp);
temp.mkdirs();
// read keep and mapping config
mBaffleConfig = new ConfigReader().read(mConfigFiles);
mObfuscateHelper = new ObfuscateHelper(mBaffleConfig);
// unzip apk or ap_ file
List<ZipInfo> zipinfos = ApkFileUtils.unZipApk(mApkFile, tempDir, toWebp ,mWebpMapping,minLevelInt);
if(removeSameFile){
removeEqualFile(temp, zipinfos);
}
// decode arsc file
mArscData = ArscData.decode(new File(tempDir + "resources.arsc"));
// do obfuscate
mObfuscateHelper.obfuscate(mArscData);
mObfuscateHelper.setWebpMapping(mWebpMapping);
// write mapping file
if (mMappingFile != null)
{
mMappingWrite = new MappingWriter(mObfuscateHelper.getObfuscateData().keyMaping);
mMappingWrite.WriteToFile(mMappingFile);
} else {
log.log(Level.CONFIG, "not specific mapping file");
}
StringBlock tableBlock = createStrings(mArscData.getmTableStrings(), true);
StringBlock keyBlock = createStrings(mArscData.getmSpecNames(), false);
File arscFile = new File(tempDir + "resources.n.arsc");
CRC32 arscCrc = mArscData.createObfuscateFile(tableBlock, keyBlock, arscFile);
mZipinfos = zipinfos;
ZipInfo arscInfo = new ZipInfo("resources.arsc", ZipEntry.STORED, arscFile.length(), arscCrc.getValue(), "");
try {
new ApkBuilder(mObfuscateHelper, mZipinfos, arscInfo).reBuildapk(mTarget, tempDir);
} catch (IOException e) {
OS.rmfile(mTarget);
throw e;
}
OS.rmdir(temp);
}
protected void processJarFile(File file) throws Exception {
if (verbose) {
log("processing " + file.toURI());
}
File tempFile = File.createTempFile(file.getName(), null, new File(file
.getAbsoluteFile().getParent()));
try{
ZipInputStream zip = new ZipInputStream(new FileInputStream(file));
try {
FileOutputStream fout = new FileOutputStream(tempFile);
try{
ZipOutputStream out = new ZipOutputStream(fout);
ZipEntry entry;
while ((entry = zip.getNextEntry()) != null) {
byte bytes[] = getBytes(zip);
if (!entry.isDirectory()) {
DataInputStream din = new DataInputStream(
new ByteArrayInputStream(bytes)
);
if (din.readInt() == CLASS_MAGIC) {
bytes = process(bytes);
} else {
if (verbose) {
log("ignoring " + entry.toString());
}
}
}
ZipEntry outEntry = new ZipEntry(entry.getName());
outEntry.setMethod(entry.getMethod());
outEntry.setComment(entry.getComment());
outEntry.setSize(bytes.length);
if(outEntry.getMethod() == ZipEntry.STORED){
CRC32 crc = new CRC32();
crc.update(bytes);
outEntry.setCrc( crc.getValue() );
outEntry.setCompressedSize(bytes.length);
}
out.putNextEntry(outEntry);
out.write(bytes);
out.closeEntry();
zip.closeEntry();
}
out.close();
}finally{
fout.close();
}
} finally {
zip.close();
}
if(file.delete()){
File newFile = new File(tempFile.getAbsolutePath());
if(!newFile.renameTo(file)){
throw new IOException("can not rename " + tempFile + " to " + file);
}
}else{
throw new IOException("can not delete " + file);
}
}finally{
tempFile.delete();
}
}
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;
}
/**
* 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;
}
/**
* Returns true if entry with specified path exists and not compressed.
*
* Note that ZipEntry does not provide information about offset so we
* cannot reliably check if entry is page-aligned. For now we are going
* take optimistic approach and rely on (1) if library was extracted
* it would have been found by the previous step (2) if library was not extracted
* but STORED and not page-aligned the installation of the app would have failed
* because of checks in PackageManagerService.
*/
private boolean isZipEntryExistsAndStored(ZipFile zipFile, String path) {
ZipEntry entry = zipFile.getEntry(path);
return entry != null && entry.getMethod() == ZipEntry.STORED;
}