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

The following examples show how to use java.util.zip.ZipEntry#STORED . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: sofa-ark   File: JarWriter.java    License: Apache License 2.0 6 votes vote down vote up
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();
        }
    }
}
 
Example 2
Source Project: android_9.0.0_r45   File: StrictJarFile.java    License: 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 3
Source Project: Stark   File: ZipUtils.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 4
Source Project: ratel   File: Androlib.java    License: Apache License 2.0 5 votes vote down vote up
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();
    }
}
 
Example 5
Source Project: bundletool   File: ZipBuilder.java    License: 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 6
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 Project: consulo   File: JBZipEntry.java    License: 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 8
@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 9
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;
}
 
Example 10
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 11
Source Project: buck   File: CustomZipOutputStream.java    License: 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 12
Source Project: buck   File: EntryAccounting.java    License: 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 13
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 Project: bundletool   File: ZipEntrySubject.java    License: Apache License 2.0 4 votes vote down vote up
public ZipEntrySubject thatIsUncompressed() {
  if (actual.getMethod() != ZipEntry.STORED) {
    failWithActual(simpleFact("expected to be uncompressed in the zip file."));
  }
  return this;
}
 
Example 15
Source Project: Baffle   File: Obfuscater.java    License: MIT License 4 votes vote down vote up
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);
	}
 
Example 16
Source Project: cglib   File: AbstractTransformTask.java    License: Apache License 2.0 4 votes vote down vote up
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();
            
        }
        
    }
 
Example 17
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 18
Source Project: bazel   File: AndroidResourceOutputs.java    License: Apache License 2.0 4 votes vote down vote up
public void setCompress(boolean compress) {
  storageMethod = compress ? ZipEntry.DEFLATED : ZipEntry.STORED;
}
 
Example 19
Source Project: consulo   File: JBZipEntry.java    License: 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;
}
 
Example 20
/**
 * 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;
}