|
24 | 24 | import java.io.OutputStream;
|
25 | 25 | import java.io.RandomAccessFile;
|
26 | 26 | import java.io.UnsupportedEncodingException;
|
| 27 | +import java.nio.charset.Charset; |
27 | 28 | import java.util.Date;
|
28 | 29 | import java.util.Hashtable;
|
29 | 30 | import java.util.Vector;
|
@@ -206,6 +207,13 @@ public class ZipOutputStream
|
206 | 207 | */
|
207 | 208 | private RandomAccessFile raf = null;
|
208 | 209 |
|
| 210 | + /** |
| 211 | + * UTF-8 encoding name. |
| 212 | + * |
| 213 | + * @since 2.4.5 |
| 214 | + */ |
| 215 | + private static final String UTF8 = "UTF-8"; |
| 216 | + |
209 | 217 | /**
|
210 | 218 | * Compression method for deflated entries.
|
211 | 219 | *
|
@@ -607,6 +615,8 @@ public void flush()
|
607 | 615 | static final byte[] Z20_bytes = ZipShort.bytes( 20 );
|
608 | 616 | static final byte[] Z8_bytes = ZipShort.bytes( 8 );
|
609 | 617 | static final byte[] Z10_bytes = ZipShort.bytes( 10 );
|
| 618 | + static final byte[] Z2048_bytes = ZipShort.bytes( 2048 ); |
| 619 | + static final byte[] Z2056_bytes = ZipShort.bytes( 2056 ); |
610 | 620 | static final byte[] LFH_SIG_bytes = LFH_SIG.getBytes();
|
611 | 621 | static final byte[] DD_SIG_bytes = DD_SIG.getBytes();
|
612 | 622 | static final byte[] CFH_SIG_bytes = CFH_SIG.getBytes();
|
@@ -650,12 +660,26 @@ protected void writeLocalFileHeader( ZipEntry ze )
|
650 | 660 | writeOut( Z20_bytes );
|
651 | 661 |
|
652 | 662 | // bit3 set to signal, we use a data descriptor
|
653 |
| - writeOut( Z8_bytes ); |
| 663 | + if ( isLefRequired() ) |
| 664 | + { |
| 665 | + writeOut(Z2056_bytes); |
| 666 | + } |
| 667 | + else |
| 668 | + { |
| 669 | + writeOut(Z8_bytes); |
| 670 | + } |
654 | 671 | }
|
655 | 672 | else
|
656 | 673 | {
|
657 | 674 | writeOut( Z10_bytes );
|
658 |
| - writeOut( ZERO ); |
| 675 | + if ( isLefRequired() ) |
| 676 | + { |
| 677 | + writeOut(Z2048_bytes); |
| 678 | + } |
| 679 | + else |
| 680 | + { |
| 681 | + writeOut(ZERO); |
| 682 | + } |
659 | 683 | }
|
660 | 684 | written += 4;
|
661 | 685 |
|
@@ -749,12 +773,26 @@ protected void writeCentralFileHeader( ZipEntry ze )
|
749 | 773 | writeOut( Z20_bytes );
|
750 | 774 |
|
751 | 775 | // bit3 set to signal, we use a data descriptor
|
752 |
| - writeOut( Z8_bytes ); |
| 776 | + if ( isLefRequired() ) |
| 777 | + { |
| 778 | + writeOut( Z2056_bytes ); |
| 779 | + } |
| 780 | + else |
| 781 | + { |
| 782 | + writeOut( Z8_bytes ); |
| 783 | + } |
753 | 784 | }
|
754 | 785 | else
|
755 | 786 | {
|
756 | 787 | writeOut( Z10_bytes );
|
757 |
| - writeOut( ZERO ); |
| 788 | + if ( isLefRequired() ) |
| 789 | + { |
| 790 | + writeOut( Z2048_bytes ); |
| 791 | + } |
| 792 | + else |
| 793 | + { |
| 794 | + writeOut( ZERO ); |
| 795 | + } |
758 | 796 | }
|
759 | 797 | written += 4;
|
760 | 798 |
|
@@ -955,4 +993,11 @@ final byte[] longBytes(long value)
|
955 | 993 | {
|
956 | 994 | return ZipLong.bytes(value, longBuffer);
|
957 | 995 | }
|
| 996 | + |
| 997 | + private boolean isLefRequired() { |
| 998 | + return encoding == null |
| 999 | + && UTF8.equals(Charset.defaultCharset().name()) |
| 1000 | + || (encoding != null && UTF8.equals(encoding)); |
| 1001 | + } |
| 1002 | + |
958 | 1003 | }
|
0 commit comments