Exemple #1
0
        //throws IOException {

        /**
         * Opens the given file for reading, assuming the specified
         * encoding for file names.
         *
         * @param f the archive.
         * @param encoding the encoding to use for file names, use null
         * for the platform's default encoding
         * @param useUnicodeExtraFields whether to use InfoZIP Unicode
         * Extra Fields (if present) to set the file names.
         *
         * @throws IOException if an error occurs while reading the file.
         */
        public ZipFile(java.io.File f, String encoding, bool useUnicodeExtraFields)
        //throws IOException
        {
            this.OFFSET_COMPARATOR     = new IAC_OFFSET_COMPARATOR(this);
            this.encoding              = encoding;
            this.zipEncoding           = ZipEncodingHelper.getZipEncoding(encoding);
            this.useUnicodeExtraFields = useUnicodeExtraFields;
            archive = new java.io.RandomAccessFile(f, "r");
            bool success = false;

            try {
                java.util.Map <ZipArchiveEntry, IAC_NameAndComment> entriesWithoutUTF8Flag = populateFromCentralDirectory();
                resolveLocalFileHeaderData(entriesWithoutUTF8Flag);
                success = true;
            } finally {
                if (!success)
                {
                    try {
                        archive.close();
                    } catch (java.io.IOException) {
                        // swallow, throw the original exception instead
                    }
                }
            }
        }
 /**
  * @param encoding the encoding to use for file names, use null
  * for the platform's default encoding
  * @param useUnicodeExtraFields whether to use InfoZIP Unicode
  * Extra Fields (if present) to set the file names.
  * @param allowStoredEntriesWithDataDescriptor whether the stream
  * will try to read STORED entries that use a data descriptor
  * @since Apache Commons Compress 1.1
  */
 public ZipArchiveInputStream(java.io.InputStream inputStream,
                              String encoding,
                              bool useUnicodeExtraFields,
                              bool allowStoredEntriesWithDataDescriptor)
 {
     zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
     this.useUnicodeExtraFields = useUnicodeExtraFields;
     inJ = new java.io.PushbackInputStream(inputStream, buf.Length);
     this.allowStoredEntriesWithDataDescriptor =
         allowStoredEntriesWithDataDescriptor;
 }
Exemple #3
0
        /**
         * Reads the central directory of the given archive and populates
         * the internal tables with ZipArchiveEntry instances.
         *
         * <p>The ZipArchiveEntrys will know all data that can be obtained from
         * the central directory alone, but not the data that requires the
         * local file header or additional data to be read.</p>
         *
         * @return a Map&lt;ZipArchiveEntry, NameAndComment>&gt; of
         * zipentries that didn't have the language encoding flag set when
         * read.
         */
        private java.util.Map <ZipArchiveEntry, IAC_NameAndComment> populateFromCentralDirectory()
        //throws IOException
        {
            java.util.HashMap <ZipArchiveEntry, IAC_NameAndComment> noUTF8Flag = new java.util.HashMap <ZipArchiveEntry, IAC_NameAndComment>();

            positionAtCentralDirectory();

            byte[] cfh = new byte[CFH_LEN];

            byte[] signatureBytes = new byte[WORD];
            archive.readFully(signatureBytes);
            long sig    = ZipLong.getValue(signatureBytes);
            long cfhSig = ZipLong.getValue(ZipArchiveOutputStream.CFH_SIG);

            if (sig != cfhSig && startsWithLocalFileHeader())
            {
                throw new java.io.IOException("central directory is empty, can't expand"
                                              + " corrupt archive.");
            }
            while (sig == cfhSig)
            {
                archive.readFully(cfh);
                int             off = 0;
                ZipArchiveEntry ze  = new ZipArchiveEntry();

                int versionMadeBy = ZipShort.getValue(cfh, off);
                off += SHORT;
                ze.setPlatform((versionMadeBy >> BYTE_SHIFT) & NIBLET_MASK);

                off += SHORT; // skip version info

                GeneralPurposeBit gpFlag  = GeneralPurposeBit.parse(cfh, off);
                bool        hasUTF8Flag   = gpFlag.usesUTF8ForNames();
                ZipEncoding entryEncoding =
                    hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
                ze.setGeneralPurposeBit(gpFlag);

                off += SHORT;

                ze.setMethod(ZipShort.getValue(cfh, off));
                off += SHORT;

                // FIXME this is actually not very cpu cycles friendly as we are converting from
                // dos to java while the underlying Sun implementation will convert
                // from java to dos time for internal storage...
                long time = ZipUtil.dosToJavaTime(ZipLong.getValue(cfh, off));
                ze.setTime(time);
                off += WORD;

                ze.setCrc(ZipLong.getValue(cfh, off));
                off += WORD;

                ze.setCompressedSize(ZipLong.getValue(cfh, off));
                off += WORD;

                ze.setSize(ZipLong.getValue(cfh, off));
                off += WORD;

                int fileNameLen = ZipShort.getValue(cfh, off);
                off += SHORT;

                int extraLen = ZipShort.getValue(cfh, off);
                off += SHORT;

                int commentLen = ZipShort.getValue(cfh, off);
                off += SHORT;

                off += SHORT; // disk number

                ze.setInternalAttributes(ZipShort.getValue(cfh, off));
                off += SHORT;

                ze.setExternalAttributes(ZipLong.getValue(cfh, off));
                off += WORD;

                byte[] fileName = new byte[fileNameLen];
                archive.readFully(fileName);
                ze.setName(entryEncoding.decode(fileName));

                // LFH offset,
                IAC_OffsetEntry offset = new IAC_OffsetEntry();
                offset.headerOffset = ZipLong.getValue(cfh, off);
                // data offset will be filled later
                entries.put(ze, offset);

                nameMap.put(ze.getName(), ze);

                byte[] cdExtraData = new byte[extraLen];
                archive.readFully(cdExtraData);
                ze.setCentralDirectoryExtra(cdExtraData);

                byte[] comment = new byte[commentLen];
                archive.readFully(comment);
                ze.setComment(entryEncoding.decode(comment));

                archive.readFully(signatureBytes);
                sig = ZipLong.getValue(signatureBytes);

                if (!hasUTF8Flag && useUnicodeExtraFields)
                {
                    noUTF8Flag.put(ze, new IAC_NameAndComment(fileName, comment));
                }
            }
            return(noUTF8Flag);
        }
 /**
  * The encoding to use for filenames and the file comment.
  *
  * <p>For a list of possible values see <a
  * href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html</a>.
  * Defaults to UTF-8.</p>
  * @param encoding the encoding to use for file names, use null
  * for the platform's default encoding
  */
 public void setEncoding(String encoding)
 {
     this.encoding    = encoding;
     this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
     useUTF8Flag     &= ZipEncodingHelper.isUTF8(encoding);
 }
        public ZipArchiveEntry getNextZipEntry() //throws IOException
        {
            if (closed || hitCentralDirectory)
            {
                return(null);
            }
            if (current != null)
            {
                closeEntry();
            }
            byte[] lfh = new byte[LFH_LEN];
            try {
                readFully(lfh);
            } catch (java.io.EOFException) {
                return(null);
            }
            ZipLong sig = new ZipLong(lfh);

            if (sig.equals(ZipLong.CFH_SIG))
            {
                hitCentralDirectory = true;
                return(null);
            }
            if (!sig.equals(ZipLong.LFH_SIG))
            {
                return(null);
            }

            int off = WORD;

            current = new ZipArchiveEntry();

            int versionMadeBy = ZipShort.getValue(lfh, off);

            off += SHORT;
            current.setPlatform((versionMadeBy >> ZipFile.BYTE_SHIFT)
                                & ZipFile.NIBLET_MASK);

            GeneralPurposeBit gpFlag  = GeneralPurposeBit.parse(lfh, off);
            bool        hasUTF8Flag   = gpFlag.usesUTF8ForNames();
            ZipEncoding entryEncoding =
                hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;

            hasDataDescriptor = gpFlag.usesDataDescriptor();
            current.setGeneralPurposeBit(gpFlag);

            off += SHORT;

            current.setMethod(ZipShort.getValue(lfh, off));
            off += SHORT;

            long time = ZipUtil.dosToJavaTime(ZipLong.getValue(lfh, off));

            current.setTime(time);
            off += WORD;

            if (!hasDataDescriptor)
            {
                current.setCrc(ZipLong.getValue(lfh, off));
                off += WORD;

                current.setCompressedSize(ZipLong.getValue(lfh, off));
                off += WORD;

                current.setSize(ZipLong.getValue(lfh, off));
                off += WORD;
            }
            else
            {
                off += 3 * WORD;
            }

            int fileNameLen = ZipShort.getValue(lfh, off);

            off += SHORT;

            int extraLen = ZipShort.getValue(lfh, off);

            off += SHORT;

            byte[] fileName = new byte[fileNameLen];
            readFully(fileName);
            current.setName(entryEncoding.decode(fileName));

            byte[] extraData = new byte[extraLen];
            readFully(extraData);
            current.setExtra(extraData);

            if (!hasUTF8Flag && useUnicodeExtraFields)
            {
                ZipUtil.setNameAndCommentFromExtraFields(current, fileName, null);
            }
            return(current);
        }