Beispiel #1
0
        /*
         * Writes entry information to the underlying stream. Data associated with
         * the entry can then be written using {@code write()}. After data is
         * written {@code closeEntry()} must be called to complete the writing of
         * the entry to the underlying stream.
         *
         * @param ze
         *            the {@code ZipEntry} to store.
         * @throws IOException
         *             If an error occurs storing the entry.
         * @see #write
         */
        public void putNextEntry(ZipEntry ze)  //throws java.io.IOException {
        {
            if (currentEntry != null)
            {
                closeEntry();
            }
            if (ze.getMethod() == STORED ||
                (compressMethod == STORED && ze.getMethod() == -1))
            {
                if (ze.crc == -1)
                {
                    /* [MSG "archive.20", "Crc mismatch"] */
                    throw new ZipException("Crc mismatch"); //$NON-NLS-1$
                }
                if (ze.size == -1 && ze.compressedSize == -1)
                {
                    /* [MSG "archive.21", "Size mismatch"] */
                    throw new ZipException("Size mismatch"); //$NON-NLS-1$
                }
                if (ze.size != ze.compressedSize && ze.compressedSize != -1 &&
                    ze.size != -1)
                {
                    /* [MSG "archive.21", "Size mismatch"] */
                    throw new ZipException("Size mismatch"); //$NON-NLS-1$
                }
            }
            /* [MSG "archive.1E", "Stream is closed"] */
            if (cDir == null)
            {
                throw new java.io.IOException("Stream is closed"); //$NON-NLS-1$
            }
            if (entries.contains(ze.name))
            {
                /* [MSG "archive.29", "Entry already exists: {0}"] */
                throw new ZipException("Entry already exists: " + ze.name); //$NON-NLS-1$
            }
            nameLength = utf8Count(ze.name);
            if (nameLength > 0xffff)
            {
                /* [MSG "archive.2A", "Name too long: {0}"] */
                throw new java.lang.IllegalArgumentException("Name too long: " + ze.name); //$NON-NLS-1$
            }

            def.setLevel(compressLevel);
            currentEntry = ze;
            entries.add(currentEntry.name);
            if (currentEntry.getMethod() == -1)
            {
                currentEntry.setMethod(compressMethod);
            }
            writeLong(outJ, ZipFile.LOCSIG);               // Entry header
            writeShort(outJ, ZIPLocalHeaderVersionNeeded); // Extraction version
            writeShort(outJ, currentEntry.getMethod() == STORED ? 0
                    : ZIPDataDescriptorFlag);
            writeShort(outJ, currentEntry.getMethod());
            if (currentEntry.getTime() == -1)
            {
                currentEntry.setTime(java.lang.SystemJ.currentTimeMillis());
            }
            writeShort(outJ, currentEntry.time);
            writeShort(outJ, currentEntry.modDate);

            if (currentEntry.getMethod() == STORED)
            {
                if (currentEntry.size == -1)
                {
                    currentEntry.size = currentEntry.compressedSize;
                }
                else if (currentEntry.compressedSize == -1)
                {
                    currentEntry.compressedSize = currentEntry.size;
                }
                writeLong(outJ, currentEntry.crc);
                writeLong(outJ, currentEntry.size);
                writeLong(outJ, currentEntry.size);
            }
            else
            {
                writeLong(outJ, 0);
                writeLong(outJ, 0);
                writeLong(outJ, 0);
            }
            writeShort(outJ, nameLength);
            if (currentEntry.extra != null)
            {
                writeShort(outJ, currentEntry.extra.Length);
            }
            else
            {
                writeShort(outJ, 0);
            }
            nameBytes = toUTF8Bytes(currentEntry.name, nameLength);
            outJ.write(nameBytes);
            if (currentEntry.extra != null)
            {
                outJ.write(currentEntry.extra);
            }
        }
Beispiel #2
0
        /*
         * Reads the next entry from this {@code ZipInputStream} or {@code null} if
         * no more entries are present.
         *
         * @return the next {@code ZipEntry} contained in the input stream.
         * @throws IOException
         *             if an {@code IOException} occurs.
         * @see ZipEntry
         */
        public ZipEntry getNextEntry()  //throws IOException {
        {
            closeEntry();
            if (entriesEnd)
            {
                return(null);
            }

            int x = 0, count = 0;

            while (count != 4)
            {
                count += x = inJ.read(hdrBuf, count, 4 - count);
                if (x == -1)
                {
                    return(null);
                }
            }
            long hdr = getLong(hdrBuf, 0);

            if (hdr == ZipFile.CENSIG)
            {
                entriesEnd = true;
                return(null);
            }
            if (hdr != ZipFile.LOCSIG)
            {
                return(null);
            }

            // Read the local header
            count = 0;
            while (count != (ZipFile.LOCHDR - ZipFile.LOCVER))
            {
                count += x = inJ.read(hdrBuf, count, (ZipFile.LOCHDR - ZipFile.LOCVER) - count);
                if (x == -1)
                {
                    throw new java.io.EOFException();
                }
            }
            int version = getShort(hdrBuf, 0) & 0xff;

            if (version > ZIPLocalHeaderVersionNeeded)
            {
                throw new ZipException("Cannot read version"); //$NON-NLS-1$
            }
            int flags = getShort(hdrBuf, ZipFile.LOCFLG - ZipFile.LOCVER);

            hasDD = ((flags & ZIPDataDescriptorFlag) == ZIPDataDescriptorFlag);
            int  cetime = getShort(hdrBuf, ZipFile.LOCTIM - ZipFile.LOCVER);
            int  cemodDate = getShort(hdrBuf, ZipFile.LOCTIM - ZipFile.LOCVER + 2);
            int  cecompressionMethod = getShort(hdrBuf, ZipFile.LOCHOW - ZipFile.LOCVER);
            long cecrc = 0, cecompressedSize = 0, cesize = -1;

            if (!hasDD)
            {
                cecrc            = getLong(hdrBuf, ZipFile.LOCCRC - ZipFile.LOCVER);
                cecompressedSize = getLong(hdrBuf, ZipFile.LOCSIZ - ZipFile.LOCVER);
                cesize           = getLong(hdrBuf, ZipFile.LOCLEN - ZipFile.LOCVER);
            }
            int flen = getShort(hdrBuf, ZipFile.LOCNAM - ZipFile.LOCVER);

            if (flen == 0)
            {
                throw new ZipException("Entry is not named"); //$NON-NLS-1$
            }
            int elen = getShort(hdrBuf, ZipFile.LOCEXT - ZipFile.LOCVER);

            count = 0;
            if (flen > nameBuf.Length)
            {
                nameBuf = new byte[flen];
                charBuf = new char[flen];
            }
            while (count != flen)
            {
                count += x = inJ.read(nameBuf, count, flen - count);
                if (x == -1)
                {
                    throw new java.io.EOFException();
                }
            }
            currentEntry = createZipEntry(org.apache.harmony.luni.util.Util.convertUTF8WithBuf(nameBuf, charBuf,
                                                                                               0, flen));
            currentEntry.time    = cetime;
            currentEntry.modDate = cemodDate;
            currentEntry.setMethod(cecompressionMethod);
            if (cesize != -1)
            {
                currentEntry.setCrc(cecrc);
                currentEntry.setSize(cesize);
                currentEntry.setCompressedSize(cecompressedSize);
            }
            if (elen > 0)
            {
                count = 0;
                byte[] e = new byte[elen];
                while (count != elen)
                {
                    count += x = inJ.read(e, count, elen - count);
                    if (x == -1)
                    {
                        throw new java.io.EOFException();
                    }
                }
                currentEntry.setExtra(e);
            }
            return(currentEntry);
        }
        /**
         * Writes entry information to the underlying stream. Data associated with
         * the entry can then be written using {@code write()}. After data is
         * written {@code closeEntry()} must be called to complete the writing of
         * the entry to the underlying stream.
         *
         * @param ze
         *            the {@code ZipEntry} to store.
         * @throws IOException
         *             If an error occurs storing the entry.
         * @see #write
         */
        public void putNextEntry(ZipEntry ze)
        {
            //throws java.io.IOException {
            if (currentEntry != null) {
                closeEntry();
            }
            if (ze.getMethod() == STORED
                    || (compressMethod == STORED && ze.getMethod() == -1)) {
                if (ze.crc == -1) {
                    /* [MSG "archive.20", "Crc mismatch"] */
                    throw new ZipException("Crc mismatch"); //$NON-NLS-1$
                }
                if (ze.size == -1 && ze.compressedSize == -1) {
                    /* [MSG "archive.21", "Size mismatch"] */
                    throw new ZipException("Size mismatch"); //$NON-NLS-1$
                }
                if (ze.size != ze.compressedSize && ze.compressedSize != -1
                        && ze.size != -1) {
                    /* [MSG "archive.21", "Size mismatch"] */
                    throw new ZipException("Size mismatch"); //$NON-NLS-1$
                }
            }
            /* [MSG "archive.1E", "Stream is closed"] */
            if (cDir == null) {
                throw new java.io.IOException("Stream is closed"); //$NON-NLS-1$
            }
            if (entries.contains(ze.name)) {
                /* [MSG "archive.29", "Entry already exists: {0}"] */
                throw new ZipException("Entry already exists: "+ ze.name); //$NON-NLS-1$
            }
            nameLength = utf8Count(ze.name);
            if (nameLength > 0xffff) {
                /* [MSG "archive.2A", "Name too long: {0}"] */
                throw new java.lang.IllegalArgumentException("Name too long: "+ ze.name); //$NON-NLS-1$
            }

            def.setLevel(compressLevel);
            currentEntry = ze;
            entries.add(currentEntry.name);
            if (currentEntry.getMethod() == -1) {
                currentEntry.setMethod(compressMethod);
            }
            writeLong(outJ, ZipFile.LOCSIG); // Entry header
            writeShort(outJ, ZIPLocalHeaderVersionNeeded); // Extraction version
            writeShort(outJ, currentEntry.getMethod() == STORED ? 0
                    : ZIPDataDescriptorFlag);
            writeShort(outJ, currentEntry.getMethod());
            if (currentEntry.getTime() == -1) {
                currentEntry.setTime(java.lang.SystemJ.currentTimeMillis());
            }
            writeShort(outJ, currentEntry.time);
            writeShort(outJ, currentEntry.modDate);

            if (currentEntry.getMethod() == STORED) {
                if (currentEntry.size == -1) {
                    currentEntry.size = currentEntry.compressedSize;
                } else if (currentEntry.compressedSize == -1) {
                    currentEntry.compressedSize = currentEntry.size;
                }
                writeLong(outJ, currentEntry.crc);
                writeLong(outJ, currentEntry.size);
                writeLong(outJ, currentEntry.size);
            } else {
                writeLong(outJ, 0);
                writeLong(outJ, 0);
                writeLong(outJ, 0);
            }
            writeShort(outJ, nameLength);
            if (currentEntry.extra != null) {
                writeShort(outJ, currentEntry.extra.Length);
            } else {
                writeShort(outJ, 0);
            }
            nameBytes = toUTF8Bytes(currentEntry.name, nameLength);
            outJ.write(nameBytes);
            if (currentEntry.extra != null) {
                outJ.write(currentEntry.extra);
            }
        }
        /**
         * Reads the next entry from this {@code ZipInputStream} or {@code null} if
         * no more entries are present.
         *
         * @return the next {@code ZipEntry} contained in the input stream.
         * @throws IOException
         *             if an {@code IOException} occurs.
         * @see ZipEntry
         */
        public ZipEntry getNextEntry()
        {
            //throws IOException {
            closeEntry();
            if (entriesEnd) {
                return null;
            }

            int x = 0, count = 0;
            while (count != 4) {
                count += x = inJ.read(hdrBuf, count, 4 - count);
                if (x == -1) {
                    return null;
                }
            }
            long hdr = getLong(hdrBuf, 0);
            if (hdr == ZipFile.CENSIG) {
                entriesEnd = true;
                return null;
            }
            if (hdr != ZipFile.LOCSIG) {
                return null;
            }

            // Read the local header
            count = 0;
            while (count != (ZipFile.LOCHDR - ZipFile.LOCVER)) {
                count += x = inJ.read(hdrBuf, count, (ZipFile.LOCHDR - ZipFile.LOCVER) - count);
                if (x == -1) {
                    throw new java.io.EOFException();
                }
            }
            int version = getShort(hdrBuf, 0) & 0xff;
            if (version > ZIPLocalHeaderVersionNeeded) {
                throw new ZipException("Cannot read version"); //$NON-NLS-1$
            }
            int flags = getShort(hdrBuf, ZipFile.LOCFLG - ZipFile.LOCVER);
            hasDD = ((flags & ZIPDataDescriptorFlag) == ZIPDataDescriptorFlag);
            int cetime = getShort(hdrBuf, ZipFile.LOCTIM - ZipFile.LOCVER);
            int cemodDate = getShort(hdrBuf, ZipFile.LOCTIM - ZipFile.LOCVER + 2);
            int cecompressionMethod = getShort(hdrBuf, ZipFile.LOCHOW - ZipFile.LOCVER);
            long cecrc = 0, cecompressedSize = 0, cesize = -1;
            if (!hasDD) {
                cecrc = getLong(hdrBuf, ZipFile.LOCCRC - ZipFile.LOCVER);
                cecompressedSize = getLong(hdrBuf, ZipFile.LOCSIZ - ZipFile.LOCVER);
                cesize = getLong(hdrBuf, ZipFile.LOCLEN - ZipFile.LOCVER);
            }
            int flen = getShort(hdrBuf,ZipFile.LOCNAM - ZipFile.LOCVER);
            if (flen == 0) {
                throw new ZipException("Entry is not named"); //$NON-NLS-1$
            }
            int elen = getShort(hdrBuf, ZipFile.LOCEXT - ZipFile.LOCVER);

            count = 0;
            if (flen > nameBuf.Length) {
                nameBuf = new byte[flen];
                charBuf = new char[flen];
            }
            while (count != flen) {
                count += x = inJ.read(nameBuf, count, flen - count);
                if (x == -1) {
                    throw new java.io.EOFException();
                }
            }
            currentEntry = createZipEntry(org.apache.harmony.luni.util.Util.convertUTF8WithBuf(nameBuf, charBuf,
                    0, flen));
            currentEntry.time = cetime;
            currentEntry.modDate = cemodDate;
            currentEntry.setMethod(cecompressionMethod);
            if (cesize != -1) {
                currentEntry.setCrc(cecrc);
                currentEntry.setSize(cesize);
                currentEntry.setCompressedSize(cecompressedSize);
            }
            if (elen > 0) {
                count = 0;
                byte[] e = new byte[elen];
                while (count != elen) {
                    count += x = inJ.read(e, count, elen - count);
                    if (x == -1) {
                        throw new java.io.EOFException();
                    }
                }
                currentEntry.setExtra(e);
            }
            return currentEntry;
        }