|
32 | 32 | import java.io.InputStream;
|
33 | 33 | import java.nio.file.Files;
|
34 | 34 | import java.nio.file.attribute.FileTime;
|
| 35 | +import java.text.DateFormat; |
| 36 | +import java.text.ParseException; |
| 37 | +import java.text.SimpleDateFormat; |
35 | 38 | import java.util.Arrays;
|
36 | 39 | import java.util.Date;
|
37 | 40 | import java.util.Enumeration;
|
38 | 41 | import java.util.Map;
|
| 42 | +import java.util.TimeZone; |
39 | 43 | import java.util.zip.ZipEntry;
|
40 | 44 | import java.util.zip.ZipInputStream;
|
41 | 45 | import java.util.zip.ZipOutputStream;
|
| 46 | + |
42 | 47 | import javax.annotation.Nonnull;
|
| 48 | + |
43 | 49 | import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
|
44 | 50 | import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
45 | 51 | import org.apache.commons.compress.archivers.zip.ZipExtraField;
|
@@ -889,18 +895,54 @@ public void testForcedFileModes()
|
889 | 895 | public void testFixedEntryModificationTime()
|
890 | 896 | throws IOException
|
891 | 897 | {
|
892 |
| - final long almostMinDosTime = 315532802000L; |
| 898 | + final long almostMinDosTime = toLocalTimeZone( 315532802000L ); |
| 899 | + |
893 | 900 | final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
|
894 | 901 | final ZipArchiver archiver = getZipArchiver( zipFile );
|
895 | 902 | archiver.setLastModifiedDate( new Date( almostMinDosTime ) );
|
896 | 903 | archiver.addDirectory( new File( "src/test/resources/zip-timestamp" ) );
|
897 | 904 | archiver.createArchive();
|
898 | 905 |
|
899 | 906 | assertTrue( zipFile.exists() );
|
900 |
| - final ZipFile zf = new ZipFile( zipFile ); |
901 |
| - assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() ); |
902 |
| - assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() ); |
903 |
| - assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() ); |
| 907 | + try ( final ZipFile zf = new ZipFile( zipFile ) ) |
| 908 | + { |
| 909 | + assertEquals( almostMinDosTime, zf.getEntry( "file-with-even-time.txt" ).getTime() ); |
| 910 | + assertEquals( almostMinDosTime, zf.getEntry( "file-with-odd-time.txt" ).getTime() ); |
| 911 | + assertEquals( almostMinDosTime, zf.getEntry( "foo/" ).getTime() ); |
| 912 | + } |
| 913 | + } |
| 914 | + |
| 915 | + /** |
| 916 | + * Takes a timestamp, turns it into a textual representation based on GMT, then translated it into a timestamp in |
| 917 | + * local timezone. This makes the test independent of the current TimeZone. The reason this is necessary is: |
| 918 | + * <ul> |
| 919 | + * <li>ZIP file format does not take timezone into account.</li> |
| 920 | + * <li>In the process of converting the ZipEntry time from the DOS date format specified by the ZIP file format, the |
| 921 | + * timestamp is converted to a Java Date object, which DOES depends of the current system TimeZone, therefore |
| 922 | + * changing the value of the Date object representing that timestamp relative to the local TimeZone.</li> |
| 923 | + * </ul> |
| 924 | + * |
| 925 | + * @param timestamp the epoch time to convert. |
| 926 | + * @return the timestamp matching the same input date but in the local TZ. |
| 927 | + */ |
| 928 | + private long toLocalTimeZone( long timestamp ) |
| 929 | + { |
| 930 | + String dateFormat = "dd-MM-yyyy hh:mm:ss a"; |
| 931 | + DateFormat formatterWithTimeZone = new SimpleDateFormat( dateFormat ); |
| 932 | + formatterWithTimeZone.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); |
| 933 | + String sDate = formatterWithTimeZone.format( new Date( timestamp ) ); |
| 934 | + |
| 935 | + DateFormat formatter = new SimpleDateFormat( dateFormat ); |
| 936 | + try |
| 937 | + { |
| 938 | + Date dateWithTimeZone = formatter.parse( sDate ); |
| 939 | + return dateWithTimeZone.getTime(); |
| 940 | + } |
| 941 | + catch ( ParseException e ) |
| 942 | + { |
| 943 | + fail( "Date '" + sDate + "' can not be parsed!" ); |
| 944 | + return 0L; |
| 945 | + } |
904 | 946 | }
|
905 | 947 |
|
906 | 948 | }
|
0 commit comments