001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.zip;
019
020import java.io.ByteArrayOutputStream;
021import java.io.File;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.nio.ByteBuffer;
027import java.nio.channels.SeekableByteChannel;
028import java.nio.file.Files;
029import java.nio.file.StandardOpenOption;
030import java.util.Calendar;
031import java.util.EnumSet;
032import java.util.HashMap;
033import java.util.LinkedList;
034import java.util.List;
035import java.util.Map;
036import java.util.zip.Deflater;
037import java.util.zip.ZipException;
038
039import org.apache.commons.compress.archivers.ArchiveEntry;
040import org.apache.commons.compress.archivers.ArchiveOutputStream;
041import org.apache.commons.compress.utils.IOUtils;
042
043import static org.apache.commons.compress.archivers.zip.ZipConstants.DATA_DESCRIPTOR_MIN_VERSION;
044import static org.apache.commons.compress.archivers.zip.ZipConstants.DEFLATE_MIN_VERSION;
045import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD;
046import static org.apache.commons.compress.archivers.zip.ZipConstants.INITIAL_VERSION;
047import static org.apache.commons.compress.archivers.zip.ZipConstants.SHORT;
048import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD;
049import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MAGIC;
050import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MAGIC_SHORT;
051import static org.apache.commons.compress.archivers.zip.ZipConstants.ZIP64_MIN_VERSION;
052import static org.apache.commons.compress.archivers.zip.ZipLong.putLong;
053import static org.apache.commons.compress.archivers.zip.ZipShort.putShort;
054
055/**
056 * Reimplementation of {@link java.util.zip.ZipOutputStream
057 * java.util.zip.ZipOutputStream} that does handle the extended
058 * functionality of this package, especially internal/external file
059 * attributes and extra fields with different layouts for local file
060 * data and central directory entries.
061 *
062 * <p>This class will try to use {@link
063 * java.nio.channels.SeekableByteChannel} when it knows that the
064 * output is going to go to a file.</p>
065 *
066 * <p>If SeekableByteChannel cannot be used, this implementation will use
067 * a Data Descriptor to store size and CRC information for {@link
068 * #DEFLATED DEFLATED} entries, this means, you don't need to
069 * calculate them yourself.  Unfortunately this is not possible for
070 * the {@link #STORED STORED} method, here setting the CRC and
071 * uncompressed size information is required before {@link
072 * #putArchiveEntry(ArchiveEntry)} can be called.</p>
073 *
074 * <p>As of Apache Commons Compress 1.3 it transparently supports Zip64
075 * extensions and thus individual entries and archives larger than 4
076 * GB or with more than 65536 entries in most cases but explicit
077 * control is provided via {@link #setUseZip64}.  If the stream can not
078 * use SeekableByteChannel and you try to write a ZipArchiveEntry of
079 * unknown size then Zip64 extensions will be disabled by default.</p>
080 *
081 * @NotThreadSafe
082 */
083public class ZipArchiveOutputStream extends ArchiveOutputStream {
084
085    static final int BUFFER_SIZE = 512;
086    private static final int LFH_SIG_OFFSET = 0;
087    private static final int LFH_VERSION_NEEDED_OFFSET = 4;
088    private static final int LFH_GPB_OFFSET = 6;
089    private static final int LFH_METHOD_OFFSET = 8;
090    private static final int LFH_TIME_OFFSET = 10;
091    private static final int LFH_CRC_OFFSET = 14;
092    private static final int LFH_COMPRESSED_SIZE_OFFSET = 18;
093    private static final int LFH_ORIGINAL_SIZE_OFFSET = 22;
094    private static final int LFH_FILENAME_LENGTH_OFFSET = 26;
095    private static final int LFH_EXTRA_LENGTH_OFFSET = 28;
096    private static final int LFH_FILENAME_OFFSET = 30;
097    private static final int CFH_SIG_OFFSET = 0;
098    private static final int CFH_VERSION_MADE_BY_OFFSET = 4;
099    private static final int CFH_VERSION_NEEDED_OFFSET = 6;
100    private static final int CFH_GPB_OFFSET = 8;
101    private static final int CFH_METHOD_OFFSET = 10;
102    private static final int CFH_TIME_OFFSET = 12;
103    private static final int CFH_CRC_OFFSET = 16;
104    private static final int CFH_COMPRESSED_SIZE_OFFSET = 20;
105    private static final int CFH_ORIGINAL_SIZE_OFFSET = 24;
106    private static final int CFH_FILENAME_LENGTH_OFFSET = 28;
107    private static final int CFH_EXTRA_LENGTH_OFFSET = 30;
108    private static final int CFH_COMMENT_LENGTH_OFFSET = 32;
109    private static final int CFH_DISK_NUMBER_OFFSET = 34;
110    private static final int CFH_INTERNAL_ATTRIBUTES_OFFSET = 36;
111    private static final int CFH_EXTERNAL_ATTRIBUTES_OFFSET = 38;
112    private static final int CFH_LFH_OFFSET = 42;
113    private static final int CFH_FILENAME_OFFSET = 46;
114
115    /** indicates if this archive is finished. protected for use in Jar implementation */
116    protected boolean finished = false;
117
118    /**
119     * Compression method for deflated entries.
120     */
121    public static final int DEFLATED = java.util.zip.ZipEntry.DEFLATED;
122
123    /**
124     * Default compression level for deflated entries.
125     */
126    public static final int DEFAULT_COMPRESSION = Deflater.DEFAULT_COMPRESSION;
127
128    /**
129     * Compression method for stored entries.
130     */
131    public static final int STORED = java.util.zip.ZipEntry.STORED;
132
133    /**
134     * default encoding for file names and comment.
135     */
136    static final String DEFAULT_ENCODING = ZipEncodingHelper.UTF8;
137
138    /**
139     * General purpose flag, which indicates that filenames are
140     * written in UTF-8.
141     * @deprecated use {@link GeneralPurposeBit#UFT8_NAMES_FLAG} instead
142     */
143    @Deprecated
144    public static final int EFS_FLAG = GeneralPurposeBit.UFT8_NAMES_FLAG;
145
146    private static final byte[] EMPTY = new byte[0];
147
148    /**
149     * Current entry.
150     */
151    private CurrentEntry entry;
152
153    /**
154     * The file comment.
155     */
156    private String comment = "";
157
158    /**
159     * Compression level for next entry.
160     */
161    private int level = DEFAULT_COMPRESSION;
162
163    /**
164     * Has the compression level changed when compared to the last
165     * entry?
166     */
167    private boolean hasCompressionLevelChanged = false;
168
169    /**
170     * Default compression method for next entry.
171     */
172    private int method = java.util.zip.ZipEntry.DEFLATED;
173
174    /**
175     * List of ZipArchiveEntries written so far.
176     */
177    private final List<ZipArchiveEntry> entries =
178        new LinkedList<>();
179
180    private final StreamCompressor streamCompressor;
181
182    /**
183     * Start of central directory.
184     */
185    private long cdOffset = 0;
186
187    /**
188     * Length of central directory.
189     */
190    private long cdLength = 0;
191
192    /**
193     * Helper, a 0 as ZipShort.
194     */
195    private static final byte[] ZERO = {0, 0};
196
197    /**
198     * Helper, a 0 as ZipLong.
199     */
200    private static final byte[] LZERO = {0, 0, 0, 0};
201
202    private static final byte[] ONE = ZipLong.getBytes(1L);
203
204    /**
205     * Holds some book-keeping data for each entry.
206     */
207    private final Map<ZipArchiveEntry, EntryMetaData> metaData =
208        new HashMap<>();
209
210    /**
211     * The encoding to use for filenames and the file comment.
212     *
213     * <p>For a list of possible values see <a
214     * 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>.
215     * Defaults to UTF-8.</p>
216     */
217    private String encoding = DEFAULT_ENCODING;
218
219    /**
220     * The zip encoding to use for filenames and the file comment.
221     *
222     * This field is of internal use and will be set in {@link
223     * #setEncoding(String)}.
224     */
225    private ZipEncoding zipEncoding =
226        ZipEncodingHelper.getZipEncoding(DEFAULT_ENCODING);
227
228
229    /**
230     * This Deflater object is used for output.
231     *
232     */
233    protected final Deflater def;
234    /**
235     * Optional random access output.
236     */
237    private final SeekableByteChannel channel;
238
239    private final OutputStream out;
240
241    /**
242     * whether to use the general purpose bit flag when writing UTF-8
243     * filenames or not.
244     */
245    private boolean useUTF8Flag = true;
246
247    /**
248     * Whether to encode non-encodable file names as UTF-8.
249     */
250    private boolean fallbackToUTF8 = false;
251
252    /**
253     * whether to create UnicodePathExtraField-s for each entry.
254     */
255    private UnicodeExtraFieldPolicy createUnicodeExtraFields = UnicodeExtraFieldPolicy.NEVER;
256
257    /**
258     * Whether anything inside this archive has used a ZIP64 feature.
259     *
260     * @since 1.3
261     */
262    private boolean hasUsedZip64 = false;
263
264    private Zip64Mode zip64Mode = Zip64Mode.AsNeeded;
265
266    private final byte[] copyBuffer = new byte[32768];
267    private final Calendar calendarInstance = Calendar.getInstance();
268
269    /**
270     * Creates a new ZIP OutputStream filtering the underlying stream.
271     * @param out the outputstream to zip
272     */
273    public ZipArchiveOutputStream(final OutputStream out) {
274        this.out = out;
275        this.channel = null;
276        def = new Deflater(level, true);
277        streamCompressor = StreamCompressor.create(out, def);
278    }
279
280    /**
281     * Creates a new ZIP OutputStream writing to a File.  Will use
282     * random access if possible.
283     * @param file the file to zip to
284     * @throws IOException on error
285     */
286    public ZipArchiveOutputStream(final File file) throws IOException {
287        def = new Deflater(level, true);
288        OutputStream o = null;
289        SeekableByteChannel _channel = null;
290        StreamCompressor _streamCompressor = null;
291        try {
292            _channel = Files.newByteChannel(file.toPath(),
293                EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.WRITE,
294                           StandardOpenOption.READ,
295                           StandardOpenOption.TRUNCATE_EXISTING));
296            // will never get opened properly when an exception is thrown so doesn't need to get closed
297            _streamCompressor = StreamCompressor.create(_channel, def); //NOSONAR
298        } catch (final IOException e) {
299            IOUtils.closeQuietly(_channel);
300            _channel = null;
301            o = new FileOutputStream(file);
302            _streamCompressor = StreamCompressor.create(o, def);
303        }
304        out = o;
305        channel = _channel;
306        streamCompressor = _streamCompressor;
307    }
308
309    /**
310     * Creates a new ZIP OutputStream writing to a SeekableByteChannel.
311     *
312     * <p>{@link
313     * org.apache.commons.compress.utils.SeekableInMemoryByteChannel}
314     * allows you to write to an in-memory archive using random
315     * access.</p>
316     *
317     * @param channel the channel to zip to
318     * @throws IOException on error
319     * @since 1.13
320     */
321    public ZipArchiveOutputStream(SeekableByteChannel channel) throws IOException {
322        this.channel = channel;
323        def = new Deflater(level, true);
324        streamCompressor = StreamCompressor.create(channel, def);
325        out = null;
326    }
327
328    /**
329     * This method indicates whether this archive is writing to a
330     * seekable stream (i.e., to a random access file).
331     *
332     * <p>For seekable streams, you don't need to calculate the CRC or
333     * uncompressed size for {@link #STORED} entries before
334     * invoking {@link #putArchiveEntry(ArchiveEntry)}.
335     * @return true if seekable
336     */
337    public boolean isSeekable() {
338        return channel != null;
339    }
340
341    /**
342     * The encoding to use for filenames and the file comment.
343     *
344     * <p>For a list of possible values see <a
345     * 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>.
346     * Defaults to UTF-8.</p>
347     * @param encoding the encoding to use for file names, use null
348     * for the platform's default encoding
349     */
350    public void setEncoding(final String encoding) {
351        this.encoding = encoding;
352        this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
353        if (useUTF8Flag && !ZipEncodingHelper.isUTF8(encoding)) {
354            useUTF8Flag = false;
355        }
356    }
357
358    /**
359     * The encoding to use for filenames and the file comment.
360     *
361     * @return null if using the platform's default character encoding.
362     */
363    public String getEncoding() {
364        return encoding;
365    }
366
367    /**
368     * Whether to set the language encoding flag if the file name
369     * encoding is UTF-8.
370     *
371     * <p>Defaults to true.</p>
372     *
373     * @param b whether to set the language encoding flag if the file
374     * name encoding is UTF-8
375     */
376    public void setUseLanguageEncodingFlag(final boolean b) {
377        useUTF8Flag = b && ZipEncodingHelper.isUTF8(encoding);
378    }
379
380    /**
381     * Whether to create Unicode Extra Fields.
382     *
383     * <p>Defaults to NEVER.</p>
384     *
385     * @param b whether to create Unicode Extra Fields.
386     */
387    public void setCreateUnicodeExtraFields(final UnicodeExtraFieldPolicy b) {
388        createUnicodeExtraFields = b;
389    }
390
391    /**
392     * Whether to fall back to UTF and the language encoding flag if
393     * the file name cannot be encoded using the specified encoding.
394     *
395     * <p>Defaults to false.</p>
396     *
397     * @param b whether to fall back to UTF and the language encoding
398     * flag if the file name cannot be encoded using the specified
399     * encoding.
400     */
401    public void setFallbackToUTF8(final boolean b) {
402        fallbackToUTF8 = b;
403    }
404
405    /**
406     * Whether Zip64 extensions will be used.
407     *
408     * <p>When setting the mode to {@link Zip64Mode#Never Never},
409     * {@link #putArchiveEntry}, {@link #closeArchiveEntry}, {@link
410     * #finish} or {@link #close} may throw a {@link
411     * Zip64RequiredException} if the entry's size or the total size
412     * of the archive exceeds 4GB or there are more than 65536 entries
413     * inside the archive.  Any archive created in this mode will be
414     * readable by implementations that don't support Zip64.</p>
415     *
416     * <p>When setting the mode to {@link Zip64Mode#Always Always},
417     * Zip64 extensions will be used for all entries.  Any archive
418     * created in this mode may be unreadable by implementations that
419     * don't support Zip64 even if all its contents would be.</p>
420     *
421     * <p>When setting the mode to {@link Zip64Mode#AsNeeded
422     * AsNeeded}, Zip64 extensions will transparently be used for
423     * those entries that require them.  This mode can only be used if
424     * the uncompressed size of the {@link ZipArchiveEntry} is known
425     * when calling {@link #putArchiveEntry} or the archive is written
426     * to a seekable output (i.e. you have used the {@link
427     * #ZipArchiveOutputStream(java.io.File) File-arg constructor}) -
428     * this mode is not valid when the output stream is not seekable
429     * and the uncompressed size is unknown when {@link
430     * #putArchiveEntry} is called.</p>
431     *
432     * <p>If no entry inside the resulting archive requires Zip64
433     * extensions then {@link Zip64Mode#Never Never} will create the
434     * smallest archive.  {@link Zip64Mode#AsNeeded AsNeeded} will
435     * create a slightly bigger archive if the uncompressed size of
436     * any entry has initially been unknown and create an archive
437     * identical to {@link Zip64Mode#Never Never} otherwise.  {@link
438     * Zip64Mode#Always Always} will create an archive that is at
439     * least 24 bytes per entry bigger than the one {@link
440     * Zip64Mode#Never Never} would create.</p>
441     *
442     * <p>Defaults to {@link Zip64Mode#AsNeeded AsNeeded} unless
443     * {@link #putArchiveEntry} is called with an entry of unknown
444     * size and data is written to a non-seekable stream - in this
445     * case the default is {@link Zip64Mode#Never Never}.</p>
446     *
447     * @since 1.3
448     * @param mode Whether Zip64 extensions will be used.
449     */
450    public void setUseZip64(final Zip64Mode mode) {
451        zip64Mode = mode;
452    }
453
454    /**
455     * {@inheritDoc}
456     * @throws Zip64RequiredException if the archive's size exceeds 4
457     * GByte or there are more than 65535 entries inside the archive
458     * and {@link #setUseZip64} is {@link Zip64Mode#Never}.
459     */
460    @Override
461    public void finish() throws IOException {
462        if (finished) {
463            throw new IOException("This archive has already been finished");
464        }
465
466        if (entry != null) {
467            throw new IOException("This archive contains unclosed entries.");
468        }
469
470        cdOffset = streamCompressor.getTotalBytesWritten();
471        writeCentralDirectoryInChunks();
472
473        cdLength = streamCompressor.getTotalBytesWritten() - cdOffset;
474        writeZip64CentralDirectory();
475        writeCentralDirectoryEnd();
476        metaData.clear();
477        entries.clear();
478        streamCompressor.close();
479        finished = true;
480    }
481
482    private void writeCentralDirectoryInChunks() throws IOException {
483        final int NUM_PER_WRITE = 1000;
484        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(70 * NUM_PER_WRITE);
485        int count = 0;
486        for (final ZipArchiveEntry ze : entries) {
487            byteArrayOutputStream.write(createCentralFileHeader(ze));
488            if (++count > NUM_PER_WRITE){
489                writeCounted(byteArrayOutputStream.toByteArray());
490                byteArrayOutputStream.reset();
491                count = 0;
492            }
493        }
494        writeCounted(byteArrayOutputStream.toByteArray());
495    }
496
497    /**
498     * Writes all necessary data for this entry.
499     * @throws IOException on error
500     * @throws Zip64RequiredException if the entry's uncompressed or
501     * compressed size exceeds 4 GByte and {@link #setUseZip64}
502     * is {@link Zip64Mode#Never}.
503     */
504    @Override
505    public void closeArchiveEntry() throws IOException {
506        preClose();
507
508        flushDeflater();
509
510        final long bytesWritten = streamCompressor.getTotalBytesWritten() - entry.dataStart;
511        final long realCrc = streamCompressor.getCrc32();
512        entry.bytesRead = streamCompressor.getBytesRead();
513        final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry);
514        final boolean actuallyNeedsZip64 = handleSizesAndCrc(bytesWritten, realCrc, effectiveMode);
515        closeEntry(actuallyNeedsZip64, false);
516        streamCompressor.reset();
517    }
518
519    /**
520     * Writes all necessary data for this entry.
521     *
522     * @param phased              This entry is second phase of a 2-phase zip creation, size, compressed size and crc
523     *                            are known in ZipArchiveEntry
524     * @throws IOException            on error
525     * @throws Zip64RequiredException if the entry's uncompressed or
526     *                                compressed size exceeds 4 GByte and {@link #setUseZip64}
527     *                                is {@link Zip64Mode#Never}.
528     */
529    private void closeCopiedEntry(final boolean phased) throws IOException {
530        preClose();
531        entry.bytesRead = entry.entry.getSize();
532        final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry);
533        final boolean actuallyNeedsZip64 = checkIfNeedsZip64(effectiveMode);
534        closeEntry(actuallyNeedsZip64, phased);
535    }
536
537    private void closeEntry(final boolean actuallyNeedsZip64, final boolean phased) throws IOException {
538        if (!phased && channel != null) {
539            rewriteSizesAndCrc(actuallyNeedsZip64);
540        }
541
542        if (!phased) {
543            writeDataDescriptor(entry.entry);
544        }
545        entry = null;
546    }
547
548    private void preClose() throws IOException {
549        if (finished) {
550            throw new IOException("Stream has already been finished");
551        }
552
553        if (entry == null) {
554            throw new IOException("No current entry to close");
555        }
556
557        if (!entry.hasWritten) {
558            write(EMPTY, 0, 0);
559        }
560    }
561
562    /**
563     * Adds an archive entry with a raw input stream.
564     *
565     * If crc, size and compressed size are supplied on the entry, these values will be used as-is.
566     * Zip64 status is re-established based on the settings in this stream, and the supplied value
567     * is ignored.
568     *
569     * The entry is put and closed immediately.
570     *
571     * @param entry The archive entry to add
572     * @param rawStream The raw input stream of a different entry. May be compressed/encrypted.
573     * @throws IOException If copying fails
574     */
575    public void addRawArchiveEntry(final ZipArchiveEntry entry, final InputStream rawStream)
576            throws IOException {
577        final ZipArchiveEntry ae = new ZipArchiveEntry(entry);
578        if (hasZip64Extra(ae)) {
579            // Will be re-added as required. this may make the file generated with this method
580            // somewhat smaller than standard mode,
581            // since standard mode is unable to remove the zip 64 header.
582            ae.removeExtraField(Zip64ExtendedInformationExtraField.HEADER_ID);
583        }
584        final boolean is2PhaseSource = ae.getCrc() != ZipArchiveEntry.CRC_UNKNOWN
585                && ae.getSize() != ArchiveEntry.SIZE_UNKNOWN
586                && ae.getCompressedSize() != ArchiveEntry.SIZE_UNKNOWN;
587        putArchiveEntry(ae, is2PhaseSource);
588        copyFromZipInputStream(rawStream);
589        closeCopiedEntry(is2PhaseSource);
590    }
591
592    /**
593     * Ensures all bytes sent to the deflater are written to the stream.
594     */
595    private void flushDeflater() throws IOException {
596        if (entry.entry.getMethod() == DEFLATED) {
597            streamCompressor.flushDeflater();
598        }
599    }
600
601    /**
602     * Ensures the current entry's size and CRC information is set to
603     * the values just written, verifies it isn't too big in the
604     * Zip64Mode.Never case and returns whether the entry would
605     * require a Zip64 extra field.
606     */
607    private boolean handleSizesAndCrc(final long bytesWritten, final long crc,
608                                      final Zip64Mode effectiveMode)
609        throws ZipException {
610        if (entry.entry.getMethod() == DEFLATED) {
611            /* It turns out def.getBytesRead() returns wrong values if
612             * the size exceeds 4 GB on Java < Java7
613            entry.entry.setSize(def.getBytesRead());
614            */
615            entry.entry.setSize(entry.bytesRead);
616            entry.entry.setCompressedSize(bytesWritten);
617            entry.entry.setCrc(crc);
618
619        } else if (channel == null) {
620            if (entry.entry.getCrc() != crc) {
621                throw new ZipException("bad CRC checksum for entry "
622                                       + entry.entry.getName() + ": "
623                                       + Long.toHexString(entry.entry.getCrc())
624                                       + " instead of "
625                                       + Long.toHexString(crc));
626            }
627
628            if (entry.entry.getSize() != bytesWritten) {
629                throw new ZipException("bad size for entry "
630                                       + entry.entry.getName() + ": "
631                                       + entry.entry.getSize()
632                                       + " instead of "
633                                       + bytesWritten);
634            }
635        } else { /* method is STORED and we used SeekableByteChannel */
636            entry.entry.setSize(bytesWritten);
637            entry.entry.setCompressedSize(bytesWritten);
638            entry.entry.setCrc(crc);
639        }
640
641        return checkIfNeedsZip64(effectiveMode);
642    }
643
644    /**
645     * Verifies the sizes aren't too big in the Zip64Mode.Never case
646     * and returns whether the entry would require a Zip64 extra
647     * field.
648     */
649    private boolean checkIfNeedsZip64(final Zip64Mode effectiveMode)
650            throws ZipException {
651        final boolean actuallyNeedsZip64 = isZip64Required(entry.entry, effectiveMode);
652        if (actuallyNeedsZip64 && effectiveMode == Zip64Mode.Never) {
653            throw new Zip64RequiredException(Zip64RequiredException.getEntryTooBigMessage(entry.entry));
654        }
655        return actuallyNeedsZip64;
656    }
657
658    private boolean isZip64Required(final ZipArchiveEntry entry1, final Zip64Mode requestedMode) {
659        return requestedMode == Zip64Mode.Always || isTooLageForZip32(entry1);
660    }
661
662    private boolean isTooLageForZip32(final ZipArchiveEntry zipArchiveEntry){
663        return zipArchiveEntry.getSize() >= ZIP64_MAGIC || zipArchiveEntry.getCompressedSize() >= ZIP64_MAGIC;
664    }
665
666    /**
667     * When using random access output, write the local file header
668     * and potentiall the ZIP64 extra containing the correct CRC and
669     * compressed/uncompressed sizes.
670     */
671    private void rewriteSizesAndCrc(final boolean actuallyNeedsZip64)
672        throws IOException {
673        final long save = channel.position();
674
675        channel.position(entry.localDataStart);
676        writeOut(ZipLong.getBytes(entry.entry.getCrc()));
677        if (!hasZip64Extra(entry.entry) || !actuallyNeedsZip64) {
678            writeOut(ZipLong.getBytes(entry.entry.getCompressedSize()));
679            writeOut(ZipLong.getBytes(entry.entry.getSize()));
680        } else {
681            writeOut(ZipLong.ZIP64_MAGIC.getBytes());
682            writeOut(ZipLong.ZIP64_MAGIC.getBytes());
683        }
684
685        if (hasZip64Extra(entry.entry)) {
686            final ByteBuffer name = getName(entry.entry);
687            final int nameLen = name.limit() - name.position();
688            // seek to ZIP64 extra, skip header and size information
689            channel.position(entry.localDataStart + 3 * WORD + 2 * SHORT
690                             + nameLen + 2 * SHORT);
691            // inside the ZIP64 extra uncompressed size comes
692            // first, unlike the LFH, CD or data descriptor
693            writeOut(ZipEightByteInteger.getBytes(entry.entry.getSize()));
694            writeOut(ZipEightByteInteger.getBytes(entry.entry.getCompressedSize()));
695
696            if (!actuallyNeedsZip64) {
697                // do some cleanup:
698                // * rewrite version needed to extract
699                channel.position(entry.localDataStart  - 5 * SHORT);
700                writeOut(ZipShort.getBytes(versionNeededToExtract(entry.entry.getMethod(), false, false)));
701
702                // * remove ZIP64 extra so it doesn't get written
703                //   to the central directory
704                entry.entry.removeExtraField(Zip64ExtendedInformationExtraField
705                                             .HEADER_ID);
706                entry.entry.setExtra();
707
708                // * reset hasUsedZip64 if it has been set because
709                //   of this entry
710                if (entry.causedUseOfZip64) {
711                    hasUsedZip64 = false;
712                }
713            }
714        }
715        channel.position(save);
716    }
717
718    /**
719     * {@inheritDoc}
720     * @throws ClassCastException if entry is not an instance of ZipArchiveEntry
721     * @throws Zip64RequiredException if the entry's uncompressed or
722     * compressed size is known to exceed 4 GByte and {@link #setUseZip64}
723     * is {@link Zip64Mode#Never}.
724     */
725    @Override
726    public void putArchiveEntry(final ArchiveEntry archiveEntry) throws IOException {
727        putArchiveEntry(archiveEntry, false);
728    }
729
730    /**
731     * Writes the headers for an archive entry to the output stream.
732     * The caller must then write the content to the stream and call
733     * {@link #closeArchiveEntry()} to complete the process.
734
735     * @param archiveEntry The archiveEntry
736     * @param phased If true size, compressedSize and crc required to be known up-front in the archiveEntry
737     * @throws ClassCastException if entry is not an instance of ZipArchiveEntry
738     * @throws Zip64RequiredException if the entry's uncompressed or
739     * compressed size is known to exceed 4 GByte and {@link #setUseZip64}
740     * is {@link Zip64Mode#Never}.
741     */
742    private void putArchiveEntry(final ArchiveEntry archiveEntry, final boolean phased) throws IOException {
743        if (finished) {
744            throw new IOException("Stream has already been finished");
745        }
746
747        if (entry != null) {
748            closeArchiveEntry();
749        }
750
751        entry = new CurrentEntry((ZipArchiveEntry) archiveEntry);
752        entries.add(entry.entry);
753
754        setDefaults(entry.entry);
755
756        final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry);
757        validateSizeInformation(effectiveMode);
758
759        if (shouldAddZip64Extra(entry.entry, effectiveMode)) {
760
761            final Zip64ExtendedInformationExtraField z64 = getZip64Extra(entry.entry);
762
763            ZipEightByteInteger size;
764            ZipEightByteInteger compressedSize;
765            if (phased) {
766                // sizes are already known
767                size = new ZipEightByteInteger(entry.entry.getSize());
768                compressedSize = new ZipEightByteInteger(entry.entry.getCompressedSize());
769            } else if (entry.entry.getMethod() == STORED
770                    && entry.entry.getSize() != ArchiveEntry.SIZE_UNKNOWN) {
771                // actually, we already know the sizes
772                compressedSize = size = new ZipEightByteInteger(entry.entry.getSize());
773            } else {
774                // just a placeholder, real data will be in data
775                // descriptor or inserted later via SeekableByteChannel
776                compressedSize = size = ZipEightByteInteger.ZERO;
777            }
778            z64.setSize(size);
779            z64.setCompressedSize(compressedSize);
780            entry.entry.setExtra();
781        }
782
783        if (entry.entry.getMethod() == DEFLATED && hasCompressionLevelChanged) {
784            def.setLevel(level);
785            hasCompressionLevelChanged = false;
786        }
787        writeLocalFileHeader((ZipArchiveEntry) archiveEntry, phased);
788    }
789
790    /**
791     * Provides default values for compression method and last
792     * modification time.
793     */
794    private void setDefaults(final ZipArchiveEntry entry) {
795        if (entry.getMethod() == -1) { // not specified
796            entry.setMethod(method);
797        }
798
799        if (entry.getTime() == -1) { // not specified
800            entry.setTime(System.currentTimeMillis());
801        }
802    }
803
804    /**
805     * Throws an exception if the size is unknown for a stored entry
806     * that is written to a non-seekable output or the entry is too
807     * big to be written without Zip64 extra but the mode has been set
808     * to Never.
809     */
810    private void validateSizeInformation(final Zip64Mode effectiveMode)
811        throws ZipException {
812        // Size/CRC not required if SeekableByteChannel is used
813        if (entry.entry.getMethod() == STORED && channel == null) {
814            if (entry.entry.getSize() == ArchiveEntry.SIZE_UNKNOWN) {
815                throw new ZipException("uncompressed size is required for"
816                                       + " STORED method when not writing to a"
817                                       + " file");
818            }
819            if (entry.entry.getCrc() == ZipArchiveEntry.CRC_UNKNOWN) {
820                throw new ZipException("crc checksum is required for STORED"
821                                       + " method when not writing to a file");
822            }
823            entry.entry.setCompressedSize(entry.entry.getSize());
824        }
825
826        if ((entry.entry.getSize() >= ZIP64_MAGIC
827             || entry.entry.getCompressedSize() >= ZIP64_MAGIC)
828            && effectiveMode == Zip64Mode.Never) {
829            throw new Zip64RequiredException(Zip64RequiredException
830                                             .getEntryTooBigMessage(entry.entry));
831        }
832    }
833
834    /**
835     * Whether to addd a Zip64 extended information extra field to the
836     * local file header.
837     *
838     * <p>Returns true if</p>
839     *
840     * <ul>
841     * <li>mode is Always</li>
842     * <li>or we already know it is going to be needed</li>
843     * <li>or the size is unknown and we can ensure it won't hurt
844     * other implementations if we add it (i.e. we can erase its
845     * usage</li>
846     * </ul>
847     */
848    private boolean shouldAddZip64Extra(final ZipArchiveEntry entry, final Zip64Mode mode) {
849        return mode == Zip64Mode.Always
850            || entry.getSize() >= ZIP64_MAGIC
851            || entry.getCompressedSize() >= ZIP64_MAGIC
852            || (entry.getSize() == ArchiveEntry.SIZE_UNKNOWN
853                && channel != null && mode != Zip64Mode.Never);
854    }
855
856    /**
857     * Set the file comment.
858     * @param comment the comment
859     */
860    public void setComment(final String comment) {
861        this.comment = comment;
862    }
863
864    /**
865     * Sets the compression level for subsequent entries.
866     *
867     * <p>Default is Deflater.DEFAULT_COMPRESSION.</p>
868     * @param level the compression level.
869     * @throws IllegalArgumentException if an invalid compression
870     * level is specified.
871     */
872    public void setLevel(final int level) {
873        if (level < Deflater.DEFAULT_COMPRESSION
874            || level > Deflater.BEST_COMPRESSION) {
875            throw new IllegalArgumentException("Invalid compression level: "
876                                               + level);
877        }
878        hasCompressionLevelChanged = (this.level != level);
879        this.level = level;
880    }
881
882    /**
883     * Sets the default compression method for subsequent entries.
884     *
885     * <p>Default is DEFLATED.</p>
886     * @param method an <code>int</code> from java.util.zip.ZipEntry
887     */
888    public void setMethod(final int method) {
889        this.method = method;
890    }
891
892    /**
893     * Whether this stream is able to write the given entry.
894     *
895     * <p>May return false if it is set up to use encryption or a
896     * compression method that hasn't been implemented yet.</p>
897     * @since 1.1
898     */
899    @Override
900    public boolean canWriteEntryData(final ArchiveEntry ae) {
901        if (ae instanceof ZipArchiveEntry) {
902            final ZipArchiveEntry zae = (ZipArchiveEntry) ae;
903            return zae.getMethod() != ZipMethod.IMPLODING.getCode()
904                && zae.getMethod() != ZipMethod.UNSHRINKING.getCode()
905                && ZipUtil.canHandleEntryData(zae);
906        }
907        return false;
908    }
909
910    /**
911     * Writes bytes to ZIP entry.
912     * @param b the byte array to write
913     * @param offset the start position to write from
914     * @param length the number of bytes to write
915     * @throws IOException on error
916     */
917    @Override
918    public void write(final byte[] b, final int offset, final int length) throws IOException {
919        if (entry == null) {
920            throw new IllegalStateException("No current entry");
921        }
922        ZipUtil.checkRequestedFeatures(entry.entry);
923        final long writtenThisTime = streamCompressor.write(b, offset, length, entry.entry.getMethod());
924        count(writtenThisTime);
925    }
926
927    /**
928     * Write bytes to output or random access file.
929     * @param data the byte array to write
930     * @throws IOException on error
931     */
932    private void writeCounted(final byte[] data) throws IOException {
933        streamCompressor.writeCounted(data);
934    }
935
936    private void copyFromZipInputStream(final InputStream src) throws IOException {
937        if (entry == null) {
938            throw new IllegalStateException("No current entry");
939        }
940        ZipUtil.checkRequestedFeatures(entry.entry);
941        entry.hasWritten = true;
942        int length;
943        while ((length = src.read(copyBuffer)) >= 0 )
944        {
945            streamCompressor.writeCounted(copyBuffer, 0, length);
946            count( length );
947        }
948    }
949
950    /**
951     * Closes this output stream and releases any system resources
952     * associated with the stream.
953     *
954     * @throws  IOException  if an I/O error occurs.
955     * @throws Zip64RequiredException if the archive's size exceeds 4
956     * GByte or there are more than 65535 entries inside the archive
957     * and {@link #setUseZip64} is {@link Zip64Mode#Never}.
958     */
959    @Override
960    public void close() throws IOException {
961        if (!finished) {
962            finish();
963        }
964        destroy();
965    }
966
967    /**
968     * Flushes this output stream and forces any buffered output bytes
969     * to be written out to the stream.
970     *
971     * @throws  IOException  if an I/O error occurs.
972     */
973    @Override
974    public void flush() throws IOException {
975        if (out != null) {
976            out.flush();
977        }
978    }
979
980    /*
981     * Various ZIP constants shared between this class, ZipArchiveInputStream and ZipFile
982     */
983    /**
984     * local file header signature
985     */
986    static final byte[] LFH_SIG = ZipLong.LFH_SIG.getBytes(); //NOSONAR
987    /**
988     * data descriptor signature
989     */
990    static final byte[] DD_SIG = ZipLong.DD_SIG.getBytes(); //NOSONAR
991    /**
992     * central file header signature
993     */
994    static final byte[] CFH_SIG = ZipLong.CFH_SIG.getBytes(); //NOSONAR
995    /**
996     * end of central dir signature
997     */
998    static final byte[] EOCD_SIG = ZipLong.getBytes(0X06054B50L); //NOSONAR
999    /**
1000     * ZIP64 end of central dir signature
1001     */
1002    static final byte[] ZIP64_EOCD_SIG = ZipLong.getBytes(0X06064B50L); //NOSONAR
1003    /**
1004     * ZIP64 end of central dir locator signature
1005     */
1006    static final byte[] ZIP64_EOCD_LOC_SIG = ZipLong.getBytes(0X07064B50L); //NOSONAR
1007
1008    /**
1009     * Writes next block of compressed data to the output stream.
1010     * @throws IOException on error
1011     */
1012    protected final void deflate() throws IOException {
1013        streamCompressor.deflate();
1014    }
1015
1016    /**
1017     * Writes the local file header entry
1018     * @param ze the entry to write
1019     * @throws IOException on error
1020     */
1021    protected void writeLocalFileHeader(final ZipArchiveEntry ze) throws IOException {
1022        writeLocalFileHeader(ze, false);
1023    }
1024
1025    private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased) throws IOException {
1026        final boolean encodable = zipEncoding.canEncode(ze.getName());
1027        final ByteBuffer name = getName(ze);
1028
1029        if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) {
1030            addUnicodeExtraFields(ze, encodable, name);
1031        }
1032
1033        final long localHeaderStart = streamCompressor.getTotalBytesWritten();
1034        final byte[] localHeader = createLocalFileHeader(ze, name, encodable, phased, localHeaderStart);
1035        metaData.put(ze, new EntryMetaData(localHeaderStart, usesDataDescriptor(ze.getMethod(), phased)));
1036        entry.localDataStart = localHeaderStart + LFH_CRC_OFFSET; // At crc offset
1037        writeCounted(localHeader);
1038        entry.dataStart = streamCompressor.getTotalBytesWritten();
1039    }
1040
1041
1042    private byte[] createLocalFileHeader(final ZipArchiveEntry ze, final ByteBuffer name, final boolean encodable,
1043                                         final boolean phased, long archiveOffset) {
1044        ResourceAlignmentExtraField oldAlignmentEx =
1045            (ResourceAlignmentExtraField) ze.getExtraField(ResourceAlignmentExtraField.ID);
1046        if (oldAlignmentEx != null) {
1047            ze.removeExtraField(ResourceAlignmentExtraField.ID);
1048        }
1049
1050        int alignment = ze.getAlignment();
1051        if (alignment <= 0 && oldAlignmentEx != null) {
1052            alignment = oldAlignmentEx.getAlignment();
1053        }
1054
1055        if (alignment > 1 || (oldAlignmentEx != null && !oldAlignmentEx.allowMethodChange())) {
1056            int oldLength = LFH_FILENAME_OFFSET +
1057                            name.limit() - name.position() +
1058                            ze.getLocalFileDataExtra().length;
1059
1060            int padding = (int) ((-archiveOffset - oldLength - ZipExtraField.EXTRAFIELD_HEADER_SIZE
1061                            - ResourceAlignmentExtraField.BASE_SIZE) &
1062                            (alignment - 1));
1063            ze.addExtraField(new ResourceAlignmentExtraField(alignment,
1064                            oldAlignmentEx != null && oldAlignmentEx.allowMethodChange(), padding));
1065        }
1066
1067        final byte[] extra = ze.getLocalFileDataExtra();
1068        final int nameLen = name.limit() - name.position();
1069        final int len = LFH_FILENAME_OFFSET + nameLen + extra.length;
1070        final byte[] buf = new byte[len];
1071
1072        System.arraycopy(LFH_SIG,  0, buf, LFH_SIG_OFFSET, WORD);
1073
1074        //store method in local variable to prevent multiple method calls
1075        final int zipMethod = ze.getMethod();
1076        final boolean dataDescriptor = usesDataDescriptor(zipMethod, phased);
1077
1078        putShort(versionNeededToExtract(zipMethod, hasZip64Extra(ze), dataDescriptor), buf, LFH_VERSION_NEEDED_OFFSET);
1079
1080        final GeneralPurposeBit generalPurposeBit = getGeneralPurposeBits(!encodable && fallbackToUTF8, dataDescriptor);
1081        generalPurposeBit.encode(buf, LFH_GPB_OFFSET);
1082
1083        // compression method
1084        putShort(zipMethod, buf, LFH_METHOD_OFFSET);
1085
1086        ZipUtil.toDosTime(calendarInstance, ze.getTime(), buf, LFH_TIME_OFFSET);
1087
1088        // CRC
1089        if (phased){
1090            putLong(ze.getCrc(), buf, LFH_CRC_OFFSET);
1091        } else if (zipMethod == DEFLATED || channel != null) {
1092            System.arraycopy(LZERO, 0, buf, LFH_CRC_OFFSET, WORD);
1093        } else {
1094            putLong(ze.getCrc(), buf, LFH_CRC_OFFSET);
1095        }
1096
1097        // compressed length
1098        // uncompressed length
1099        if (hasZip64Extra(entry.entry)){
1100            // point to ZIP64 extended information extra field for
1101            // sizes, may get rewritten once sizes are known if
1102            // stream is seekable
1103            ZipLong.ZIP64_MAGIC.putLong(buf, LFH_COMPRESSED_SIZE_OFFSET);
1104            ZipLong.ZIP64_MAGIC.putLong(buf, LFH_ORIGINAL_SIZE_OFFSET);
1105        } else if (phased) {
1106            putLong(ze.getCompressedSize(), buf, LFH_COMPRESSED_SIZE_OFFSET);
1107            putLong(ze.getSize(), buf, LFH_ORIGINAL_SIZE_OFFSET);
1108        } else if (zipMethod == DEFLATED || channel != null) {
1109            System.arraycopy(LZERO, 0, buf, LFH_COMPRESSED_SIZE_OFFSET, WORD);
1110            System.arraycopy(LZERO, 0, buf, LFH_ORIGINAL_SIZE_OFFSET, WORD);
1111        } else { // Stored
1112            putLong(ze.getSize(), buf, LFH_COMPRESSED_SIZE_OFFSET);
1113            putLong(ze.getSize(), buf, LFH_ORIGINAL_SIZE_OFFSET);
1114        }
1115        // file name length
1116        putShort(nameLen, buf, LFH_FILENAME_LENGTH_OFFSET);
1117
1118        // extra field length
1119        putShort(extra.length, buf, LFH_EXTRA_LENGTH_OFFSET);
1120
1121        // file name
1122        System.arraycopy( name.array(), name.arrayOffset(), buf, LFH_FILENAME_OFFSET, nameLen);
1123
1124        // extra fields
1125        System.arraycopy(extra, 0, buf, LFH_FILENAME_OFFSET + nameLen, extra.length);
1126
1127        return buf;
1128    }
1129
1130
1131    /**
1132     * Adds UnicodeExtra fields for name and file comment if mode is
1133     * ALWAYS or the data cannot be encoded using the configured
1134     * encoding.
1135     */
1136    private void addUnicodeExtraFields(final ZipArchiveEntry ze, final boolean encodable,
1137                                       final ByteBuffer name)
1138        throws IOException {
1139        if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS
1140            || !encodable) {
1141            ze.addExtraField(new UnicodePathExtraField(ze.getName(),
1142                                                       name.array(),
1143                                                       name.arrayOffset(),
1144                                                       name.limit()
1145                                                       - name.position()));
1146        }
1147
1148        final String comm = ze.getComment();
1149        if (comm != null && !"".equals(comm)) {
1150
1151            final boolean commentEncodable = zipEncoding.canEncode(comm);
1152
1153            if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS
1154                || !commentEncodable) {
1155                final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
1156                ze.addExtraField(new UnicodeCommentExtraField(comm,
1157                                                              commentB.array(),
1158                                                              commentB.arrayOffset(),
1159                                                              commentB.limit()
1160                                                              - commentB.position())
1161                                 );
1162            }
1163        }
1164    }
1165
1166    /**
1167     * Writes the data descriptor entry.
1168     * @param ze the entry to write
1169     * @throws IOException on error
1170     */
1171    protected void writeDataDescriptor(final ZipArchiveEntry ze) throws IOException {
1172        if (!usesDataDescriptor(ze.getMethod(), false)) {
1173            return;
1174        }
1175        writeCounted(DD_SIG);
1176        writeCounted(ZipLong.getBytes(ze.getCrc()));
1177        if (!hasZip64Extra(ze)) {
1178            writeCounted(ZipLong.getBytes(ze.getCompressedSize()));
1179            writeCounted(ZipLong.getBytes(ze.getSize()));
1180        } else {
1181            writeCounted(ZipEightByteInteger.getBytes(ze.getCompressedSize()));
1182            writeCounted(ZipEightByteInteger.getBytes(ze.getSize()));
1183        }
1184    }
1185
1186    /**
1187     * Writes the central file header entry.
1188     * @param ze the entry to write
1189     * @throws IOException on error
1190     * @throws Zip64RequiredException if the archive's size exceeds 4
1191     * GByte and {@link Zip64Mode #setUseZip64} is {@link
1192     * Zip64Mode#Never}.
1193     */
1194    protected void writeCentralFileHeader(final ZipArchiveEntry ze) throws IOException {
1195        final byte[] centralFileHeader = createCentralFileHeader(ze);
1196        writeCounted(centralFileHeader);
1197    }
1198
1199    private byte[] createCentralFileHeader(final ZipArchiveEntry ze) throws IOException {
1200
1201        final EntryMetaData entryMetaData = metaData.get(ze);
1202        final boolean needsZip64Extra = hasZip64Extra(ze)
1203                || ze.getCompressedSize() >= ZIP64_MAGIC
1204                || ze.getSize() >= ZIP64_MAGIC
1205                || entryMetaData.offset >= ZIP64_MAGIC
1206                || zip64Mode == Zip64Mode.Always;
1207
1208        if (needsZip64Extra && zip64Mode == Zip64Mode.Never) {
1209            // must be the offset that is too big, otherwise an
1210            // exception would have been throw in putArchiveEntry or
1211            // closeArchiveEntry
1212            throw new Zip64RequiredException(Zip64RequiredException
1213                    .ARCHIVE_TOO_BIG_MESSAGE);
1214        }
1215
1216
1217        handleZip64Extra(ze, entryMetaData.offset, needsZip64Extra);
1218
1219        return createCentralFileHeader(ze, getName(ze), entryMetaData, needsZip64Extra);
1220    }
1221
1222    /**
1223     * Writes the central file header entry.
1224     * @param ze the entry to write
1225     * @param name The encoded name
1226     * @param entryMetaData meta data for this file
1227     * @throws IOException on error
1228     */
1229    private byte[] createCentralFileHeader(final ZipArchiveEntry ze, final ByteBuffer name,
1230                                           final EntryMetaData entryMetaData,
1231                                           final boolean needsZip64Extra) throws IOException {
1232        final byte[] extra = ze.getCentralDirectoryExtra();
1233
1234        // file comment length
1235        String comm = ze.getComment();
1236        if (comm == null) {
1237            comm = "";
1238        }
1239
1240        final ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
1241        final int nameLen = name.limit() - name.position();
1242        final int commentLen = commentB.limit() - commentB.position();
1243        final int len= CFH_FILENAME_OFFSET + nameLen + extra.length + commentLen;
1244        final byte[] buf = new byte[len];
1245
1246        System.arraycopy(CFH_SIG,  0, buf, CFH_SIG_OFFSET, WORD);
1247
1248        // version made by
1249        // CheckStyle:MagicNumber OFF
1250        putShort((ze.getPlatform() << 8) | (!hasUsedZip64 ? DATA_DESCRIPTOR_MIN_VERSION : ZIP64_MIN_VERSION),
1251                buf, CFH_VERSION_MADE_BY_OFFSET);
1252
1253        final int zipMethod = ze.getMethod();
1254        final boolean encodable = zipEncoding.canEncode(ze.getName());
1255        putShort(versionNeededToExtract(zipMethod, needsZip64Extra, entryMetaData.usesDataDescriptor),
1256            buf, CFH_VERSION_NEEDED_OFFSET);
1257        getGeneralPurposeBits(!encodable && fallbackToUTF8, entryMetaData.usesDataDescriptor).encode(buf, CFH_GPB_OFFSET);
1258
1259        // compression method
1260        putShort(zipMethod, buf, CFH_METHOD_OFFSET);
1261
1262
1263        // last mod. time and date
1264        ZipUtil.toDosTime(calendarInstance, ze.getTime(), buf, CFH_TIME_OFFSET);
1265
1266        // CRC
1267        // compressed length
1268        // uncompressed length
1269        putLong(ze.getCrc(), buf, CFH_CRC_OFFSET);
1270        if (ze.getCompressedSize() >= ZIP64_MAGIC
1271                || ze.getSize() >= ZIP64_MAGIC
1272                || zip64Mode == Zip64Mode.Always) {
1273            ZipLong.ZIP64_MAGIC.putLong(buf, CFH_COMPRESSED_SIZE_OFFSET);
1274            ZipLong.ZIP64_MAGIC.putLong(buf, CFH_ORIGINAL_SIZE_OFFSET);
1275        } else {
1276            putLong(ze.getCompressedSize(), buf, CFH_COMPRESSED_SIZE_OFFSET);
1277            putLong(ze.getSize(), buf, CFH_ORIGINAL_SIZE_OFFSET);
1278        }
1279
1280        putShort(nameLen, buf, CFH_FILENAME_LENGTH_OFFSET);
1281
1282        // extra field length
1283        putShort(extra.length, buf, CFH_EXTRA_LENGTH_OFFSET);
1284
1285        putShort(commentLen, buf, CFH_COMMENT_LENGTH_OFFSET);
1286
1287        // disk number start
1288        System.arraycopy(ZERO, 0, buf, CFH_DISK_NUMBER_OFFSET, SHORT);
1289
1290        // internal file attributes
1291        putShort(ze.getInternalAttributes(), buf, CFH_INTERNAL_ATTRIBUTES_OFFSET);
1292
1293        // external file attributes
1294        putLong(ze.getExternalAttributes(), buf, CFH_EXTERNAL_ATTRIBUTES_OFFSET);
1295
1296        // relative offset of LFH
1297        if (entryMetaData.offset >= ZIP64_MAGIC || zip64Mode == Zip64Mode.Always) {
1298            putLong(ZIP64_MAGIC, buf, CFH_LFH_OFFSET);
1299        } else {
1300            putLong(Math.min(entryMetaData.offset, ZIP64_MAGIC), buf, CFH_LFH_OFFSET);
1301        }
1302
1303        // file name
1304        System.arraycopy(name.array(), name.arrayOffset(), buf, CFH_FILENAME_OFFSET, nameLen);
1305
1306        final int extraStart = CFH_FILENAME_OFFSET + nameLen;
1307        System.arraycopy(extra, 0, buf, extraStart, extra.length);
1308
1309        final int commentStart = extraStart + extra.length;
1310
1311        // file comment
1312        System.arraycopy(commentB.array(), commentB.arrayOffset(), buf, commentStart, commentLen);
1313        return buf;
1314    }
1315
1316    /**
1317     * If the entry needs Zip64 extra information inside the central
1318     * directory then configure its data.
1319     */
1320    private void handleZip64Extra(final ZipArchiveEntry ze, final long lfhOffset,
1321                                  final boolean needsZip64Extra) {
1322        if (needsZip64Extra) {
1323            final Zip64ExtendedInformationExtraField z64 = getZip64Extra(ze);
1324            if (ze.getCompressedSize() >= ZIP64_MAGIC
1325                || ze.getSize() >= ZIP64_MAGIC
1326                || zip64Mode == Zip64Mode.Always) {
1327                z64.setCompressedSize(new ZipEightByteInteger(ze.getCompressedSize()));
1328                z64.setSize(new ZipEightByteInteger(ze.getSize()));
1329            } else {
1330                // reset value that may have been set for LFH
1331                z64.setCompressedSize(null);
1332                z64.setSize(null);
1333            }
1334            if (lfhOffset >= ZIP64_MAGIC || zip64Mode == Zip64Mode.Always) {
1335                z64.setRelativeHeaderOffset(new ZipEightByteInteger(lfhOffset));
1336            }
1337            ze.setExtra();
1338        }
1339    }
1340
1341    /**
1342     * Writes the &quot;End of central dir record&quot;.
1343     * @throws IOException on error
1344     * @throws Zip64RequiredException if the archive's size exceeds 4
1345     * GByte or there are more than 65535 entries inside the archive
1346     * and {@link Zip64Mode #setUseZip64} is {@link Zip64Mode#Never}.
1347     */
1348    protected void writeCentralDirectoryEnd() throws IOException {
1349        writeCounted(EOCD_SIG);
1350
1351        // disk numbers
1352        writeCounted(ZERO);
1353        writeCounted(ZERO);
1354
1355        // number of entries
1356        final int numberOfEntries = entries.size();
1357        if (numberOfEntries > ZIP64_MAGIC_SHORT
1358            && zip64Mode == Zip64Mode.Never) {
1359            throw new Zip64RequiredException(Zip64RequiredException
1360                                             .TOO_MANY_ENTRIES_MESSAGE);
1361        }
1362        if (cdOffset > ZIP64_MAGIC && zip64Mode == Zip64Mode.Never) {
1363            throw new Zip64RequiredException(Zip64RequiredException
1364                                             .ARCHIVE_TOO_BIG_MESSAGE);
1365        }
1366
1367        final byte[] num = ZipShort.getBytes(Math.min(numberOfEntries,
1368                                                ZIP64_MAGIC_SHORT));
1369        writeCounted(num);
1370        writeCounted(num);
1371
1372        // length and location of CD
1373        writeCounted(ZipLong.getBytes(Math.min(cdLength, ZIP64_MAGIC)));
1374        writeCounted(ZipLong.getBytes(Math.min(cdOffset, ZIP64_MAGIC)));
1375
1376        // ZIP file comment
1377        final ByteBuffer data = this.zipEncoding.encode(comment);
1378        final int dataLen = data.limit() - data.position();
1379        writeCounted(ZipShort.getBytes(dataLen));
1380        streamCompressor.writeCounted(data.array(), data.arrayOffset(), dataLen);
1381    }
1382
1383    /**
1384     * Writes the &quot;ZIP64 End of central dir record&quot; and
1385     * &quot;ZIP64 End of central dir locator&quot;.
1386     * @throws IOException on error
1387     * @since 1.3
1388     */
1389    protected void writeZip64CentralDirectory() throws IOException {
1390        if (zip64Mode == Zip64Mode.Never) {
1391            return;
1392        }
1393
1394        if (!hasUsedZip64
1395            && (cdOffset >= ZIP64_MAGIC || cdLength >= ZIP64_MAGIC
1396                || entries.size() >= ZIP64_MAGIC_SHORT)) {
1397            // actually "will use"
1398            hasUsedZip64 = true;
1399        }
1400
1401        if (!hasUsedZip64) {
1402            return;
1403        }
1404
1405        final long offset = streamCompressor.getTotalBytesWritten();
1406
1407        writeOut(ZIP64_EOCD_SIG);
1408        // size, we don't have any variable length as we don't support
1409        // the extensible data sector, yet
1410        writeOut(ZipEightByteInteger
1411                 .getBytes(SHORT   /* version made by */
1412                           + SHORT /* version needed to extract */
1413                           + WORD  /* disk number */
1414                           + WORD  /* disk with central directory */
1415                           + DWORD /* number of entries in CD on this disk */
1416                           + DWORD /* total number of entries */
1417                           + DWORD /* size of CD */
1418                           + (long) DWORD /* offset of CD */
1419                           ));
1420
1421        // version made by and version needed to extract
1422        writeOut(ZipShort.getBytes(ZIP64_MIN_VERSION));
1423        writeOut(ZipShort.getBytes(ZIP64_MIN_VERSION));
1424
1425        // disk numbers - four bytes this time
1426        writeOut(LZERO);
1427        writeOut(LZERO);
1428
1429        // number of entries
1430        final byte[] num = ZipEightByteInteger.getBytes(entries.size());
1431        writeOut(num);
1432        writeOut(num);
1433
1434        // length and location of CD
1435        writeOut(ZipEightByteInteger.getBytes(cdLength));
1436        writeOut(ZipEightByteInteger.getBytes(cdOffset));
1437
1438        // no "zip64 extensible data sector" for now
1439
1440        // and now the "ZIP64 end of central directory locator"
1441        writeOut(ZIP64_EOCD_LOC_SIG);
1442
1443        // disk number holding the ZIP64 EOCD record
1444        writeOut(LZERO);
1445        // relative offset of ZIP64 EOCD record
1446        writeOut(ZipEightByteInteger.getBytes(offset));
1447        // total number of disks
1448        writeOut(ONE);
1449    }
1450
1451    /**
1452     * Write bytes to output or random access file.
1453     * @param data the byte array to write
1454     * @throws IOException on error
1455     */
1456    protected final void writeOut(final byte[] data) throws IOException {
1457        streamCompressor.writeOut(data, 0, data.length);
1458    }
1459
1460
1461    /**
1462     * Write bytes to output or random access file.
1463     * @param data the byte array to write
1464     * @param offset the start position to write from
1465     * @param length the number of bytes to write
1466     * @throws IOException on error
1467     */
1468    protected final void writeOut(final byte[] data, final int offset, final int length)
1469            throws IOException {
1470        streamCompressor.writeOut(data, offset, length);
1471    }
1472
1473
1474    private GeneralPurposeBit getGeneralPurposeBits(final boolean utfFallback, boolean usesDataDescriptor) {
1475        final GeneralPurposeBit b = new GeneralPurposeBit();
1476        b.useUTF8ForNames(useUTF8Flag || utfFallback);
1477        if (usesDataDescriptor) {
1478            b.useDataDescriptor(true);
1479        }
1480        return b;
1481    }
1482
1483    private int versionNeededToExtract(final int zipMethod, final boolean zip64, final boolean usedDataDescriptor) {
1484        if (zip64) {
1485            return ZIP64_MIN_VERSION;
1486        }
1487        if (usedDataDescriptor) {
1488            return DATA_DESCRIPTOR_MIN_VERSION;
1489        }
1490        return versionNeededToExtractMethod(zipMethod);
1491    }
1492
1493    private boolean usesDataDescriptor(final int zipMethod, boolean phased) {
1494        return !phased && zipMethod == DEFLATED && channel == null;
1495    }
1496
1497    private int versionNeededToExtractMethod(int zipMethod) {
1498        return zipMethod == DEFLATED ? DEFLATE_MIN_VERSION : INITIAL_VERSION;
1499    }
1500
1501    /**
1502     * Creates a new zip entry taking some information from the given
1503     * file and using the provided name.
1504     *
1505     * <p>The name will be adjusted to end with a forward slash "/" if
1506     * the file is a directory.  If the file is not a directory a
1507     * potential trailing forward slash will be stripped from the
1508     * entry name.</p>
1509     *
1510     * <p>Must not be used if the stream has already been closed.</p>
1511     */
1512    @Override
1513    public ArchiveEntry createArchiveEntry(final File inputFile, final String entryName)
1514        throws IOException {
1515        if (finished) {
1516            throw new IOException("Stream has already been finished");
1517        }
1518        return new ZipArchiveEntry(inputFile, entryName);
1519    }
1520
1521    /**
1522     * Get the existing ZIP64 extended information extra field or
1523     * create a new one and add it to the entry.
1524     *
1525     * @since 1.3
1526     */
1527    private Zip64ExtendedInformationExtraField
1528        getZip64Extra(final ZipArchiveEntry ze) {
1529        if (entry != null) {
1530            entry.causedUseOfZip64 = !hasUsedZip64;
1531        }
1532        hasUsedZip64 = true;
1533        Zip64ExtendedInformationExtraField z64 =
1534            (Zip64ExtendedInformationExtraField)
1535            ze.getExtraField(Zip64ExtendedInformationExtraField
1536                             .HEADER_ID);
1537        if (z64 == null) {
1538            /*
1539              System.err.println("Adding z64 for " + ze.getName()
1540              + ", method: " + ze.getMethod()
1541              + " (" + (ze.getMethod() == STORED) + ")"
1542              + ", channel: " + (channel != null));
1543            */
1544            z64 = new Zip64ExtendedInformationExtraField();
1545        }
1546
1547        // even if the field is there already, make sure it is the first one
1548        ze.addAsFirstExtraField(z64);
1549
1550        return z64;
1551    }
1552
1553    /**
1554     * Is there a ZIP64 extended information extra field for the
1555     * entry?
1556     *
1557     * @since 1.3
1558     */
1559    private boolean hasZip64Extra(final ZipArchiveEntry ze) {
1560        return ze.getExtraField(Zip64ExtendedInformationExtraField
1561                                .HEADER_ID)
1562            != null;
1563    }
1564
1565    /**
1566     * If the mode is AsNeeded and the entry is a compressed entry of
1567     * unknown size that gets written to a non-seekable stream then
1568     * change the default to Never.
1569     *
1570     * @since 1.3
1571     */
1572    private Zip64Mode getEffectiveZip64Mode(final ZipArchiveEntry ze) {
1573        if (zip64Mode != Zip64Mode.AsNeeded
1574            || channel != null
1575            || ze.getMethod() != DEFLATED
1576            || ze.getSize() != ArchiveEntry.SIZE_UNKNOWN) {
1577            return zip64Mode;
1578        }
1579        return Zip64Mode.Never;
1580    }
1581
1582    private ZipEncoding getEntryEncoding(final ZipArchiveEntry ze) {
1583        final boolean encodable = zipEncoding.canEncode(ze.getName());
1584        return !encodable && fallbackToUTF8
1585            ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
1586    }
1587
1588    private ByteBuffer getName(final ZipArchiveEntry ze) throws IOException {
1589        return getEntryEncoding(ze).encode(ze.getName());
1590    }
1591
1592    /**
1593     * Closes the underlying stream/file without finishing the
1594     * archive, the result will likely be a corrupt archive.
1595     *
1596     * <p>This method only exists to support tests that generate
1597     * corrupt archives so they can clean up any temporary files.</p>
1598     */
1599    void destroy() throws IOException {
1600        if (channel != null) {
1601            channel.close();
1602        }
1603        if (out != null) {
1604            out.close();
1605        }
1606    }
1607
1608    /**
1609     * enum that represents the possible policies for creating Unicode
1610     * extra fields.
1611     */
1612    public static final class UnicodeExtraFieldPolicy {
1613        /**
1614         * Always create Unicode extra fields.
1615         */
1616        public static final UnicodeExtraFieldPolicy ALWAYS = new UnicodeExtraFieldPolicy("always");
1617        /**
1618         * Never create Unicode extra fields.
1619         */
1620        public static final UnicodeExtraFieldPolicy NEVER = new UnicodeExtraFieldPolicy("never");
1621        /**
1622         * Create Unicode extra fields for filenames that cannot be
1623         * encoded using the specified encoding.
1624         */
1625        public static final UnicodeExtraFieldPolicy NOT_ENCODEABLE =
1626            new UnicodeExtraFieldPolicy("not encodeable");
1627
1628        private final String name;
1629        private UnicodeExtraFieldPolicy(final String n) {
1630            name = n;
1631        }
1632        @Override
1633        public String toString() {
1634            return name;
1635        }
1636    }
1637
1638    /**
1639     * Structure collecting information for the entry that is
1640     * currently being written.
1641     */
1642    private static final class CurrentEntry {
1643        private CurrentEntry(final ZipArchiveEntry entry) {
1644            this.entry = entry;
1645        }
1646        /**
1647         * Current ZIP entry.
1648         */
1649        private final ZipArchiveEntry entry;
1650        /**
1651         * Offset for CRC entry in the local file header data for the
1652         * current entry starts here.
1653         */
1654        private long localDataStart = 0;
1655        /**
1656         * Data for local header data
1657         */
1658        private long dataStart = 0;
1659        /**
1660         * Number of bytes read for the current entry (can't rely on
1661         * Deflater#getBytesRead) when using DEFLATED.
1662         */
1663        private long bytesRead = 0;
1664        /**
1665         * Whether current entry was the first one using ZIP64 features.
1666         */
1667        private boolean causedUseOfZip64 = false;
1668        /**
1669         * Whether write() has been called at all.
1670         *
1671         * <p>In order to create a valid archive {@link
1672         * #closeArchiveEntry closeArchiveEntry} will write an empty
1673         * array to get the CRC right if nothing has been written to
1674         * the stream at all.</p>
1675         */
1676        private boolean hasWritten;
1677    }
1678
1679    private static final class EntryMetaData {
1680        private final long offset;
1681        private final boolean usesDataDescriptor;
1682        private EntryMetaData(long offset, boolean usesDataDescriptor) {
1683            this.offset = offset;
1684            this.usesDataDescriptor = usesDataDescriptor;
1685        }
1686    }
1687}