⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zipoutputstream.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    /**     * Sets the default compression method for subsequent entries.     *     * <p>Default is DEFLATED.</p>     * @param method an <code>int</code> from java.util.zip.ZipEntry     * @since 1.1     */    public void setMethod(int method) {        this.method = method;    }    /**     * Writes bytes to ZIP entry.     * @param b the byte array to write     * @param offset the start position to write from     * @param length the number of bytes to write     * @throws IOException on error     */    public void write(byte[] b, int offset, int length) throws IOException {        if (entry.getMethod() == DEFLATED) {            if (length > 0) {                if (!def.finished()) {                    def.setInput(b, offset, length);                    while (!def.needsInput()) {                        deflate();                    }                }            }        } else {            writeOut(b, offset, length);            written += length;        }        crc.update(b, offset, length);    }    /**     * Writes a single byte to ZIP entry.     *     * <p>Delegates to the three arg method.</p>     * @param b the byte to write     * @since 1.14     * @throws IOException on error     */    public void write(int b) throws IOException {        byte[] buff = new byte[1];        buff[0] = (byte) (b & BYTE_MASK);        write(buff, 0, 1);    }    /**     * Closes this output stream and releases any system resources     * associated with the stream.     *     * @exception  IOException  if an I/O error occurs.     * @since 1.14     */    public void close() throws IOException {        finish();        if (raf != null) {            raf.close();        }        if (out != null) {            out.close();        }    }    /**     * Flushes this output stream and forces any buffered output bytes     * to be written out to the stream.     *     * @exception  IOException  if an I/O error occurs.     * @since 1.14     */    public void flush() throws IOException {        if (out != null) {            out.flush();        }    }    /*     * Various ZIP constants     */    /**     * local file header signature     *     * @since 1.1     */    protected static final byte[] LFH_SIG = ZipLong.getBytes(0X04034B50L);    /**     * data descriptor signature     *     * @since 1.1     */    protected static final byte[] DD_SIG = ZipLong.getBytes(0X08074B50L);    /**     * central file header signature     *     * @since 1.1     */    protected static final byte[] CFH_SIG = ZipLong.getBytes(0X02014B50L);    /**     * end of central dir signature     *     * @since 1.1     */    protected static final byte[] EOCD_SIG = ZipLong.getBytes(0X06054B50L);    /**     * Writes next block of compressed data to the output stream.     * @throws IOException on error     *     * @since 1.14     */    protected final void deflate() throws IOException {        int len = def.deflate(buf, 0, buf.length);        if (len > 0) {            writeOut(buf, 0, len);        }    }    /**     * Writes the local file header entry     * @param ze the entry to write     * @throws IOException on error     *     * @since 1.1     */    protected void writeLocalFileHeader(ZipEntry ze) throws IOException {        offsets.put(ze, ZipLong.getBytes(written));        writeOut(LFH_SIG);        written += WORD;        //store method in local variable to prevent multiple method calls        final int zipMethod = ze.getMethod();        // version needed to extract        // general purpose bit flag        // CheckStyle:MagicNumber OFF        if (zipMethod == DEFLATED && raf == null) {            // requires version 2 as we are going to store length info            // in the data descriptor            writeOut(ZipShort.getBytes(20));            // bit3 set to signal, we use a data descriptor            writeOut(ZipShort.getBytes(8));        } else {            writeOut(ZipShort.getBytes(10));            writeOut(ZERO);        }        // CheckStyle:MagicNumber ON        written += WORD;        // compression method        writeOut(ZipShort.getBytes(zipMethod));        written += SHORT;        // last mod. time and date        writeOut(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        byte[] name = getBytes(ze.getName());        writeOut(ZipShort.getBytes(name.length));        written += SHORT;        // extra field length        byte[] extra = ze.getLocalFileDataExtra();        writeOut(ZipShort.getBytes(extra.length));        written += SHORT;        // file name        writeOut(name);        written += name.length;        // extra field        writeOut(extra);        written += extra.length;        dataStart = written;    }    /**     * Writes the data descriptor entry.     * @param ze the entry to write     * @throws IOException on error     *     * @since 1.1     */    protected void writeDataDescriptor(ZipEntry 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    }    /**     * Writes the central file header entry.     * @param ze the entry to write     * @throws IOException on error     *     * @since 1.1     */    protected void writeCentralFileHeader(ZipEntry ze) throws IOException {        writeOut(CFH_SIG);        written += WORD;        // version made by        // CheckStyle:MagicNumber OFF        writeOut(ZipShort.getBytes((ze.getPlatform() << 8) | 20));        written += SHORT;        // 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(ZipShort.getBytes(20));            // bit3 set to signal, we use a data descriptor            writeOut(ZipShort.getBytes(8));        } else {            writeOut(ZipShort.getBytes(10));            writeOut(ZERO);        }        // CheckStyle:MagicNumber ON        written += WORD;        // compression method        writeOut(ZipShort.getBytes(ze.getMethod()));        written += SHORT;        // last mod. time and date        writeOut(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        byte[] name = getBytes(ze.getName());        writeOut(ZipShort.getBytes(name.length));        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 = "";        }        byte[] commentB = getBytes(comm);        writeOut(ZipShort.getBytes(commentB.length));        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(name);        written += name.length;        // extra field        writeOut(extra);        written += extra.length;        // file comment        writeOut(commentB);        written += commentB.length;    }    /**     * Writes the &quot;End of central dir record&quot;.     * @throws IOException on error     *     * @since 1.1     */    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        byte[] data = getBytes(comment);        writeOut(ZipShort.getBytes(data.length));        writeOut(data);    }    /**     * Smallest date/time ZIP can handle.     *     * @since 1.1     */    private static final byte[] DOS_TIME_MIN = ZipLong.getBytes(0x00002100L);    /**     * Convert a Date object to a DOS date/time field.     * @param time the <code>Date</code> to convert     * @return the date as a <code>ZipLong</code>     * @since 1.1     */    protected static ZipLong toDosTime(Date time) {        return new ZipLong(toDosTime(time.getTime()));    }    /**     * 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     * @since 1.26     */    protected static byte[] toDosTime(long t) {        Date time = new Date(t);        // CheckStyle:MagicNumberCheck OFF - I do not think that using constants        //                                   here will improve the readablity        int year = time.getYear() + 1900;        if (year < 1980) {            return DOS_TIME_MIN;        }        int month = time.getMonth() + 1;        long value =  ((year - 1980) << 25)            |         (month << 21)            |         (time.getDate() << 16)            |         (time.getHours() << 11)            |         (time.getMinutes() << 5)            |         (time.getSeconds() >> 1);        return ZipLong.getBytes(value);        // CheckStyle:MagicNumberCheck ON    }    /**     * Retrieve the bytes for the given String in the encoding set for     * this Stream.     * @param name the string to get bytes from     * @return the bytes as a byte array     * @throws ZipException on error     *     * @since 1.3     */    protected byte[] getBytes(String name) throws ZipException {        if (encoding == null) {            return name.getBytes();        } else {            try {                return name.getBytes(encoding);            } catch (UnsupportedEncodingException uee) {                throw new ZipException(uee.getMessage());            }        }    }    /**     * Write bytes to output or random access file.     * @param data the byte array to write     * @throws IOException on error     *     * @since 1.14     */    protected final void writeOut(byte[] data) throws IOException {        writeOut(data, 0, data.length);    }    /**     * Write bytes to output or random access file.     * @param data the byte array to write     * @param offset the start position to write from     * @param length the number of bytes to write     * @throws IOException on error     *     * @since 1.14     */    protected final void writeOut(byte[] data, int offset, int length)        throws IOException {        if (raf != null) {            raf.write(data, offset, length);        } else {            out.write(data, offset, length);        }    }    /**     * Assumes a negative integer really is a positive integer that     * has wrapped around and re-creates the original value.     * @param i the value to treat as unsigned int.     * @return the unsigned int as a long.     * @since 1.34     */    protected static long adjustToLong(int i) {        if (i < 0) {            return 2 * ((long) Integer.MAX_VALUE) + 2 + i;        } else {            return i;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -