예제 #1
0
        /**
         * The actual data to put into local file data - without Header-ID
         * or length specifier.
         * @return get the data
         */
        public byte[] getLocalFileDataData()
        {
            // CRC will be added later
            byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);

            byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
            // CheckStyle:MagicNumber OFF
            java.lang.SystemJ.arraycopy(ZipLong.getBytes(linkArray.Length),
                                        0, data, 2, WORD);

            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getUserId()),
                                        0, data, 6, 2);
            java.lang.SystemJ.arraycopy(ZipShort.getBytes(getGroupId()),
                                        0, data, 8, 2);

            java.lang.SystemJ.arraycopy(linkArray, 0, data, 10, linkArray.Length);
            // CheckStyle:MagicNumber ON

            crc.reset();
            crc.update(data);
            long checksum = crc.getValue();

            byte[] result = new byte[data.Length + WORD];
            java.lang.SystemJ.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
            java.lang.SystemJ.arraycopy(data, 0, result, WORD, data.Length);
            return(result);
        }
        private void assembleData()
        {
            if (unicodeName == null)
            {
                return;
            }

            data = new byte[5 + unicodeName.Length];
            // version 1
            data[0] = 0x01;
            java.lang.SystemJ.arraycopy(ZipLong.getBytes(nameCRC32), 0, data, 1, 4);
            java.lang.SystemJ.arraycopy(unicodeName, 0, data, 5, unicodeName.Length);
        }
예제 #3
0
 /**
  * Writes the data descriptor entry.
  * @param ze the entry to write
  * @throws IOException on error
  */
 protected void writeDataDescriptor(ZipArchiveEntry ze) //throws IOException
 {
     if (ze.getMethod() != DEFLATED || raf != null)
     {
         return;
     }
     writeOut(DD_SIG);
     writeOut(ZipLong.getBytes(entry.getCrc()));
     writeOut(ZipLong.getBytes(entry.getCompressedSize()));
     writeOut(ZipLong.getBytes(entry.getSize()));
     // CheckStyle:MagicNumber OFF
     written += 16;
     // CheckStyle:MagicNumber ON
 }
예제 #4
0
        private void readDataDescriptor() //throws IOException
        {
            byte[] b = new byte[WORD];
            readFully(b);
            ZipLong val = new ZipLong(b);

            if (ZipLong.DD_SIG.equals(val))
            {
                // data descriptor with signature, skip sig
                readFully(b);
                val = new ZipLong(b);
            }
            current.setCrc(val.getValue());
            readFully(b);
            current.setCompressedSize(new ZipLong(b).getValue());
            readFully(b);
            current.setSize(new ZipLong(b).getValue());
        }
예제 #5
0
        /**
         * Searches for the "End of central dir record", parses
         * it and positions the stream at the first central directory
         * record.
         */
        private void positionAtCentralDirectory()
        //throws IOException
        {
            bool found         = false;
            long off           = archive.length() - MIN_EOCD_SIZE;
            long stopSearching = java.lang.Math.max(0L, archive.length() - MAX_EOCD_SIZE);

            if (off >= 0)
            {
                archive.seek(off);
                byte[] sig  = ZipArchiveOutputStream.EOCD_SIG;
                int    curr = archive.read();
                while (off >= stopSearching && curr != -1)
                {
                    if (curr == sig[POS_0])
                    {
                        curr = archive.read();
                        if (curr == sig[POS_1])
                        {
                            curr = archive.read();
                            if (curr == sig[POS_2])
                            {
                                curr = archive.read();
                                if (curr == sig[POS_3])
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }
                    archive.seek(--off);
                    curr = archive.read();
                }
            }
            if (!found)
            {
                throw new java.util.zip.ZipException("archive is not a ZIP archive");
            }
            archive.seek(off + CFD_LOCATOR_OFFSET);
            byte[] cfdOffset = new byte[WORD];
            archive.readFully(cfdOffset);
            archive.seek(ZipLong.getValue(cfdOffset));
        }
예제 #6
0
        /**
         * Convert a Date object to a DOS date/time field.
         *
         * <p>Stolen from InfoZip's <code>fileio.c</code></p>
         * @param t number of milliseconds since the epoch
         * @return the date as a byte array
         */
        public static byte[] toDosTime(long t)
        {
            java.util.Calendar c = java.util.Calendar.getInstance();
            c.setTimeInMillis(t);

            int year = c.get(java.util.Calendar.YEAR);

            if (year < 1980)
            {
                return(copy(DOS_TIME_MIN)); // stop callers from changing the array
            }
            int  month = c.get(java.util.Calendar.MONTH) + 1;
            long value = ((year - 1980) << 25)
                         | (month << 21)
                         | (c.get(java.util.Calendar.DAY_OF_MONTH) << 16)
                         | (c.get(java.util.Calendar.HOUR_OF_DAY) << 11)
                         | (c.get(java.util.Calendar.MINUTE) << 5)
                         | (c.get(java.util.Calendar.SECOND) >> 1);

            return(ZipLong.getBytes(value));
        }
        /** {@inheritDoc} */
        public void parseFromLocalFileData(byte[] buffer, int offset, int length)
        //throws ZipException
        {
            if (length < 5)
            {
                throw new java.util.zip.ZipException("UniCode path extra data must have at least 5 bytes.");
            }

            int version = buffer[offset];

            if (version != 0x01)
            {
                throw new java.util.zip.ZipException("Unsupported version [" + version
                                                     + "] for UniCode path extra data.");
            }

            nameCRC32   = ZipLong.getValue(buffer, offset + 1);
            unicodeName = new byte[length - 5];
            java.lang.SystemJ.arraycopy(buffer, offset + 5, unicodeName, 0, length - 5);
            data = null;
        }
예제 #8
0
        /**
         * Writes the &quot;End of central dir record&quot;.
         * @throws IOException on error
         */
        protected void writeCentralDirectoryEnd() //throws IOException
        {
            writeOut(EOCD_SIG);

            // disk numbers
            writeOut(ZERO);
            writeOut(ZERO);

            // number of entries
            byte[] num = ZipShort.getBytes(entries.size());
            writeOut(num);
            writeOut(num);

            // length and location of CD
            writeOut(ZipLong.getBytes(cdLength));
            writeOut(ZipLong.getBytes(cdOffset));

            // ZIP file comment
            java.nio.ByteBuffer data = this.zipEncoding.encode(comment);
            writeOut(ZipShort.getBytes(data.limit()));
            writeOut((byte[])data.array(), data.arrayOffset(), data.limit());
        }
예제 #9
0
        /**
         * Populate data from this array as if it was in local file data.
         * @param data an array of bytes
         * @param offset the start offset
         * @param length the number of bytes in the array from offset
         * @throws ZipException on error
         */
        public void parseFromLocalFileData(byte[] data, int offset, int length)
        //throws ZipException
        {
            long givenChecksum = ZipLong.getValue(data, offset);

            byte[] tmp = new byte[length - WORD];
            java.lang.SystemJ.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
            crc.reset();
            crc.update(tmp);
            long realChecksum = crc.getValue();

            if (givenChecksum != realChecksum)
            {
                throw new java.util.zip.ZipException("bad CRC checksum "
                                                     + java.lang.Long.toHexString(givenChecksum)
                                                     + " instead of "
                                                     + java.lang.Long.toHexString(realChecksum));
            }

            int newMode = ZipShort.getValue(tmp, 0);

            // CheckStyle:MagicNumber OFF
            byte[] linkArray = new byte[(int)ZipLong.getValue(tmp, 2)];
            uid = ZipShort.getValue(tmp, 6);
            gid = ZipShort.getValue(tmp, 8);

            if (linkArray.Length == 0)
            {
                link = "";
            }
            else
            {
                java.lang.SystemJ.arraycopy(tmp, 10, linkArray, 0, linkArray.Length);
                link = System.Text.ASCIIEncoding.ASCII.GetString(linkArray);  // Uses default charset - see class Javadoc
            }
            // CheckStyle:MagicNumber ON
            setDirectory((newMode & UnixStat.DIR_FLAG) != 0);
            setMode(newMode);
        }
예제 #10
0
        /**
         * Writes all necessary data for this entry.
         * @throws IOException on error
         */
        public override void closeArchiveEntry() //throws IOException
        {
            if (finished)
            {
                throw new java.io.IOException("Stream has already been finished");
            }

            if (entry == null)
            {
                throw new java.io.IOException("No current entry to close");
            }

            long realCrc = crc.getValue();

            crc.reset();

            if (entry.getMethod() == DEFLATED)
            {
                def.finish();
                while (!def.finished())
                {
                    deflate();
                }

                entry.setSize(ZipUtil.adjustToLong(def.getTotalIn()));
                entry.setCompressedSize(ZipUtil.adjustToLong(def.getTotalOut()));
                entry.setCrc(realCrc);

                def.reset();

                written += entry.getCompressedSize();
            }
            else if (raf == null)
            {
                if (entry.getCrc() != realCrc)
                {
                    throw new java.util.zip.ZipException("bad CRC checksum for entry "
                                                         + entry.getName() + ": "
                                                         + java.lang.Long.toHexString(entry.getCrc())
                                                         + " instead of "
                                                         + java.lang.Long.toHexString(realCrc));
                }

                if (entry.getSize() != written - dataStart)
                {
                    throw new java.util.zip.ZipException("bad size for entry "
                                                         + entry.getName() + ": "
                                                         + entry.getSize()
                                                         + " instead of "
                                                         + (written - dataStart));
                }
            }
            else     /* method is STORED and we used RandomAccessFile */
            {
                long size = written - dataStart;

                entry.setSize(size);
                entry.setCompressedSize(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(ZipLong.getBytes(entry.getCrc()));
                writeOut(ZipLong.getBytes(entry.getCompressedSize()));
                writeOut(ZipLong.getBytes(entry.getSize()));
                raf.seek(save);
            }

            writeDataDescriptor(entry);
            entry = null;
        }
예제 #11
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);
        }
예제 #12
0
 /**
  * Create instance from bytes.
  * @param bytes the bytes to store as a ZipLong
  */
 public ZipLong(byte[] bytes)
 {
     value = ZipLong.getValue(bytes, 0);
 }
예제 #13
0
        /**
         * Writes the central file header entry.
         * @param ze the entry to write
         * @throws IOException on error
         */
        protected void writeCentralFileHeader(ZipArchiveEntry ze) //throws IOException
        {
            writeOut(CFH_SIG);
            written += WORD;

            // version made by
            // CheckStyle:MagicNumber OFF
            writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20));
            written += SHORT;

            int  zipMethod = ze.getMethod();
            bool encodable = zipEncoding.canEncode(ze.getName());

            writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod,
                                                             !encodable &&
                                                             fallbackToUTF8);
            written += WORD;

            // compression method
            writeOut(ZipShort.getBytes(zipMethod));
            written += SHORT;

            // last mod. time and date
            writeOut(ZipUtil.toDosTime(ze.getTime()));
            written += WORD;

            // CRC
            // compressed length
            // uncompressed length
            writeOut(ZipLong.getBytes(ze.getCrc()));
            writeOut(ZipLong.getBytes(ze.getCompressedSize()));
            writeOut(ZipLong.getBytes(ze.getSize()));
            // CheckStyle:MagicNumber OFF
            written += 12;
            // CheckStyle:MagicNumber ON

            // file name length
            ZipEncoding entryEncoding;

            if (!encodable && fallbackToUTF8)
            {
                entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
            }
            else
            {
                entryEncoding = zipEncoding;
            }

            java.nio.ByteBuffer name = entryEncoding.encode(ze.getName());

            writeOut(ZipShort.getBytes(name.limit()));
            written += SHORT;

            // extra field length
            byte[] extra = ze.getCentralDirectoryExtra();
            writeOut(ZipShort.getBytes(extra.Length));
            written += SHORT;

            // file comment length
            String comm = ze.getComment();

            if (comm == null)
            {
                comm = "";
            }

            java.nio.ByteBuffer commentB = entryEncoding.encode(comm);

            writeOut(ZipShort.getBytes(commentB.limit()));
            written += SHORT;

            // disk number start
            writeOut(ZERO);
            written += SHORT;

            // internal file attributes
            writeOut(ZipShort.getBytes(ze.getInternalAttributes()));
            written += SHORT;

            // external file attributes
            writeOut(ZipLong.getBytes(ze.getExternalAttributes()));
            written += WORD;

            // relative offset of LFH
            writeOut((byte[])offsets.get(ze));
            written += WORD;

            // file name
            writeOut((byte[])name.array(), name.arrayOffset(), name.limit());
            written += name.limit();

            // extra field
            writeOut(extra);
            written += extra.Length;

            // file comment
            writeOut((byte[])commentB.array(), commentB.arrayOffset(), commentB.limit());
            written += commentB.limit();
        }
예제 #14
0
 /**
  * Create instance from the four bytes starting at offset.
  * @param bytes the bytes to store as a ZipLong
  * @param offset the offset to start
  */
 public ZipLong(byte[] bytes, int offset)
 {
     value = ZipLong.getValue(bytes, offset);
 }
예제 #15
0
        /**
         * Writes the local file header entry
         * @param ze the entry to write
         * @throws IOException on error
         */
        protected void writeLocalFileHeader(ZipArchiveEntry ze) //throws IOException
        {
            bool encodable = zipEncoding.canEncode(ze.getName());

            ZipEncoding entryEncoding;

            if (!encodable && fallbackToUTF8)
            {
                entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
            }
            else
            {
                entryEncoding = zipEncoding;
            }

            java.nio.ByteBuffer name = entryEncoding.encode(ze.getName());

            if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER)
            {
                if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS ||
                    !encodable)
                {
                    ze.addExtraField(new UnicodePathExtraField(ze.getName(),
                                                               (byte[])name.array(),
                                                               name.arrayOffset(),
                                                               name.limit()));
                }

                String comm = ze.getComment();
                if (comm != null && !"".equals(comm))
                {
                    bool commentEncodable = this.zipEncoding.canEncode(comm);

                    if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS ||
                        !commentEncodable)
                    {
                        java.nio.ByteBuffer commentB = entryEncoding.encode(comm);
                        ze.addExtraField(new UnicodeCommentExtraField(comm,
                                                                      (byte[])commentB.array(),
                                                                      commentB.arrayOffset(),
                                                                      commentB.limit())
                                         );
                    }
                }
            }

            offsets.put(ze, ZipLong.getBytes(written));

            writeOut(LFH_SIG);
            written += WORD;

            //store method in local variable to prevent multiple method calls
            int zipMethod = ze.getMethod();

            writeVersionNeededToExtractAndGeneralPurposeBits(zipMethod,
                                                             !encodable &&
                                                             fallbackToUTF8);
            written += WORD;

            // compression method
            writeOut(ZipShort.getBytes(zipMethod));
            written += SHORT;

            // last mod. time and date
            writeOut(ZipUtil.toDosTime(ze.getTime()));
            written += WORD;

            // CRC
            // compressed length
            // uncompressed length
            localDataStart = written;
            if (zipMethod == DEFLATED || raf != null)
            {
                writeOut(LZERO);
                writeOut(LZERO);
                writeOut(LZERO);
            }
            else
            {
                writeOut(ZipLong.getBytes(ze.getCrc()));
                writeOut(ZipLong.getBytes(ze.getSize()));
                writeOut(ZipLong.getBytes(ze.getSize()));
            }
            // CheckStyle:MagicNumber OFF
            written += 12;
            // CheckStyle:MagicNumber ON

            // file name length
            writeOut(ZipShort.getBytes(name.limit()));
            written += SHORT;

            // extra field length
            byte[] extra = ze.getLocalFileDataExtra();
            writeOut(ZipShort.getBytes(extra.Length));
            written += SHORT;

            // file name
            writeOut((byte[])name.array(), name.arrayOffset(), name.limit());
            written += name.limit();

            // extra field
            writeOut(extra);
            written += extra.Length;

            dataStart = written;
        }
예제 #16
0
 /**
  * Get value as four bytes in big endian byte order.
  * @return value as four bytes in big endian order
  */
 public byte[] getBytes()
 {
     return(ZipLong.getBytes(value));
 }
예제 #17
0
        /**
         * Convert a DOS date/time field to a Date object.
         *
         * @param zipDosTime contains the stored DOS time.
         * @return a Date instance corresponding to the given time.
         */
        public static java.util.Date fromDosTime(ZipLong zipDosTime)
        {
            long dosTime = zipDosTime.getValue();

            return(new java.util.Date(dosToJavaTime(dosTime)));
        }
예제 #18
0
        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);
        }