Esempio n. 1
0
        /*
         * Closes the current {@code ZipEntry}. Any entry terminal data is written
         * to the underlying stream.
         *
         * @throws IOException
         *             If an error occurs closing the entry.
         */
        public void closeEntry() // throws IOException {
        {
            if (cDir == null)
            {
                throw new java.io.IOException("Stream is closed"); //$NON-NLS-1$
            }
            if (currentEntry == null)
            {
                return;
            }
            if (currentEntry.getMethod() == DEFLATED)
            {
                base.finish();
            }

            // Verify values for STORED types
            if (currentEntry.getMethod() == STORED)
            {
                if (crc.getValue() != currentEntry.crc)
                {
                    throw new ZipException("Crc mismatch"); //$NON-NLS-1$
                }
                if (currentEntry.size != crc.tbytes)
                {
                    throw new ZipException("Size mismatch"); //$NON-NLS-1$
                }
            }
            curOffset = ZipFile.LOCHDR;

            // Write the DataDescriptor
            if (currentEntry.getMethod() != STORED)
            {
                curOffset += ZipFile.EXTHDR;
                writeLong(outJ, ZipFile.EXTSIG);
                writeLong(outJ, currentEntry.crc            = crc.getValue());
                writeLong(outJ, currentEntry.compressedSize = def.getTotalOut());
                writeLong(outJ, currentEntry.size           = def.getTotalIn());
            }
            // Update the CentralDirectory
            writeLong(cDir, ZipFile.CENSIG);
            writeShort(cDir, ZIPLocalHeaderVersionNeeded); // Version created
            writeShort(cDir, ZIPLocalHeaderVersionNeeded); // Version to extract
            writeShort(cDir, currentEntry.getMethod() == STORED ? 0
                    : ZIPDataDescriptorFlag);
            writeShort(cDir, currentEntry.getMethod());
            writeShort(cDir, currentEntry.time);
            writeShort(cDir, currentEntry.modDate);
            writeLong(cDir, crc.getValue());
            if (currentEntry.getMethod() == DEFLATED)
            {
                curOffset += (int)writeLong(cDir, def.getTotalOut());
                writeLong(cDir, def.getTotalIn());
            }
            else
            {
                curOffset += (int)writeLong(cDir, crc.tbytes);
                writeLong(cDir, crc.tbytes);
            }
            curOffset += writeShort(cDir, nameLength);
            if (currentEntry.extra != null)
            {
                curOffset += (int)writeShort(cDir, currentEntry.extra.Length);
            }
            else
            {
                writeShort(cDir, 0);
            }
            String c;

            if ((c = currentEntry.getComment()) != null)
            {
                writeShort(cDir, c.length());
            }
            else
            {
                writeShort(cDir, 0);
            }
            writeShort(cDir, 0); // Disk Start
            writeShort(cDir, 0); // Internal File Attributes
            writeLong(cDir, 0);  // External File Attributes
            writeLong(cDir, offset);
            cDir.write(nameBytes);
            nameBytes = null;
            if (currentEntry.extra != null)
            {
                cDir.write(currentEntry.extra);
            }
            offset += curOffset;
            if (c != null)
            {
                cDir.write(c.getBytes());
            }
            currentEntry = null;
            crc.reset();
            def.reset();
            done = false;
        }
Esempio n. 2
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);
            }
        }
        /**
         * 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);
            }
        }