protected void execute(String path, File outputDirectory) throws ArchiverException { ZipFile zipFile = null; try { zipFile = new ZipFile(getSourceFile(), encoding); Enumeration e = zipFile.getEntries(); while (e.hasMoreElements()) { ZipEntry ze = (ZipEntry) e.nextElement(); if (ze.getName().startsWith(path)) { extractFileIfIncluded( getSourceFile(), outputDirectory, zipFile.getInputStream(ze), ze.getName(), new Date(ze.getTime()), ze.isDirectory(), null); } } } catch (IOException ioe) { throw new ArchiverException( "Error while expanding " + getSourceFile().getAbsolutePath(), ioe); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e) { // ignore } } } }
/** * 解压缩功能. 将zipFilePath解压到saveDir目录 * * @throws Exception */ public static void upZipFile(String zipFilePath, String saveDir) throws Exception { ZipFile zfile = null; try { zfile = new ZipFile(zipFilePath); Enumeration zList = zfile.getEntries(); ZipEntry ze = null; byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { File f = new File(saveDir + File.separatorChar + ze.getName()); f.mkdir(); continue; } OutputStream os = null; InputStream is = null; try { os = new BufferedOutputStream( new FileOutputStream( getRealFileName(saveDir + File.separatorChar, ze.getName()))); is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } } finally { is.close(); os.close(); } } } finally { zfile.close(); } }
protected void execute() throws ArchiverException { getLogger().info("Expanding: " + getSourceFile() + " into " + getDestDirectory()); ZipFile zf = null; try { zf = new ZipFile(getSourceFile(), encoding); Enumeration e = zf.getEntries(); while (e.hasMoreElements()) { ZipEntry ze = (ZipEntry) e.nextElement(); extractFileIfIncluded( getSourceFile(), getDestDirectory(), zf.getInputStream(ze), ze.getName(), new Date(ze.getTime()), ze.isDirectory(), null); } getLogger().debug("expand complete"); } catch (IOException ioe) { throw new ArchiverException( "Error while expanding " + getSourceFile().getAbsolutePath(), ioe); } finally { if (zf != null) { try { zf.close(); } catch (IOException e) { // ignore } } } }
/** * Writes all necessary data for this entry. * * @since 1.1 */ public void closeEntry() throws IOException { if (entry == null) { return; } long realCrc = crc.getValue(); crc.reset(); if (entry.getMethod() == DEFLATED) { def.finish(); while (!def.finished()) { deflate(); } entry.setSize(def.getTotalIn()); entry.setComprSize(def.getTotalOut()); entry.setCrc(realCrc); def.reset(); written += entry.getCompressedSize(); } else if (raf == null) { if (entry.getCrc() != realCrc) { throw new SwcException.BadCRC(Long.toHexString(entry.getCrc()), Long.toHexString(realCrc)); } if (entry.getSize() != written - dataStart) { throw new SwcException.BadZipSize( entry.getName(), entry.getSize() + "", (written - dataStart) + ""); } } else { /* method is STORED and we used RandomAccessFile */ long size = written - dataStart; entry.setSize(size); entry.setComprSize(size); entry.setCrc(realCrc); } // If random access output, write the local file header containing // the correct CRC and compressed/uncompressed sizes if (raf != null) { long save = raf.getFilePointer(); raf.seek(localDataStart); writeOut((new ZipLong(entry.getCrc())).getBytes()); writeOut((new ZipLong(entry.getCompressedSize())).getBytes()); writeOut((new ZipLong(entry.getSize())).getBytes()); raf.seek(save); } writeDataDescriptor(entry); entry = null; }
/** * If the entry has Unicode*ExtraFields and the CRCs of the names/comments match those of the * extra fields, transfer the known Unicode values from the extra field. */ static void setNameAndCommentFromExtraFields( ZipEntry ze, byte[] originalNameBytes, byte[] commentBytes) { UnicodePathExtraField name = (UnicodePathExtraField) ze.getExtraField(UnicodePathExtraField.UPATH_ID); String originalName = ze.getName(); String newName = getUnicodeStringIfOriginalMatches(name, originalNameBytes); if (newName != null && !originalName.equals(newName)) { ze.setName(newName); } if (commentBytes != null && commentBytes.length > 0) { UnicodeCommentExtraField cmt = (UnicodeCommentExtraField) ze.getExtraField(UnicodeCommentExtraField.UCOM_ID); String newComment = getUnicodeStringIfOriginalMatches(cmt, commentBytes); if (newComment != null) { ze.setComment(newComment); } } }
/** * Writes the central file header entry * * @since 1.1 */ protected void writeCentralFileHeader(ZipEntry ze) throws IOException { writeOut(CFH_SIG.getBytes()); written += 4; // version made by writeOut((new ZipShort((ze.getPlatform() << 8) | 20)).getBytes()); written += 2; // version needed to extract // general purpose bit flag if (ze.getMethod() == DEFLATED && raf == null) { // requires version 2 as we are going to store length info // in the data descriptor writeOut((new ZipShort(20)).getBytes()); // bit3 set to signal, we use a data descriptor writeOut((new ZipShort(8)).getBytes()); } else { writeOut((new ZipShort(10)).getBytes()); writeOut(ZERO); } written += 4; // compression method writeOut((new ZipShort(ze.getMethod())).getBytes()); written += 2; // last mod. time and date writeOut(toDosTime(ze.getTime()).getBytes()); written += 4; // CRC // compressed length // uncompressed length writeOut((new ZipLong(ze.getCrc())).getBytes()); writeOut((new ZipLong(ze.getCompressedSize())).getBytes()); writeOut((new ZipLong(ze.getSize())).getBytes()); written += 12; // file name length byte[] name = getBytes(ze.getName()); writeOut((new ZipShort(name.length)).getBytes()); written += 2; // extra field length byte[] extra = ze.getCentralDirectoryExtra(); writeOut((new ZipShort(extra.length)).getBytes()); written += 2; // file comment length String comm = ze.getComment(); if (comm == null) { comm = ""; } byte[] comment = getBytes(comm); writeOut((new ZipShort(comment.length)).getBytes()); written += 2; // disk number start writeOut(ZERO); written += 2; // internal file attributes writeOut((new ZipShort(ze.getInternalAttributes())).getBytes()); written += 2; // external file attributes writeOut((new ZipLong(ze.getExternalAttributes())).getBytes()); written += 4; // relative offset of LFH writeOut(offsets.get(ze).getBytes()); written += 4; // file name writeOut(name); written += name.length; // extra field writeOut(extra); written += extra.length; // file comment writeOut(comment); written += comment.length; }
/** * Writes the local file header entry * * @since 1.1 */ protected void writeLocalFileHeader(ZipEntry ze) throws IOException { offsets.put(ze, new ZipLong(written)); writeOut(LFH_SIG.getBytes()); written += 4; // version needed to extract // general purpose bit flag if (ze.getMethod() == DEFLATED && raf == null) { // requires version 2 as we are going to store length info // in the data descriptor writeOut((new ZipShort(20)).getBytes()); // bit3 set to signal, we use a data descriptor writeOut((new ZipShort(8)).getBytes()); } else { writeOut((new ZipShort(10)).getBytes()); writeOut(ZERO); } written += 4; // compression method writeOut((new ZipShort(ze.getMethod())).getBytes()); written += 2; // last mod. time and date writeOut(toDosTime(ze.getTime()).getBytes()); written += 4; // CRC // compressed length // uncompressed length localDataStart = written; if (ze.getMethod() == DEFLATED || raf != null) { writeOut(LZERO); writeOut(LZERO); writeOut(LZERO); } else { writeOut((new ZipLong(ze.getCrc())).getBytes()); writeOut((new ZipLong(ze.getSize())).getBytes()); writeOut((new ZipLong(ze.getSize())).getBytes()); } written += 12; // file name length byte[] name = getBytes(ze.getName()); writeOut((new ZipShort(name.length)).getBytes()); written += 2; // extra field length byte[] extra = ze.getLocalFileDataExtra(); writeOut((new ZipShort(extra.length)).getBytes()); written += 2; // file name writeOut(name); written += name.length; // extra field writeOut(extra); written += extra.length; dataStart = written; }