Skip to content

Commit 4e7bda8

Browse files
committed
Merge branch 'belingueres-ISSUE-122'
2 parents 0300fa8 + 7e2cda5 commit 4e7bda8

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/test/java/org/codehaus/plexus/archiver/zip/ZipArchiverTest.java

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@
3232
import java.io.InputStream;
3333
import java.nio.file.Files;
3434
import java.nio.file.attribute.FileTime;
35+
import java.text.DateFormat;
36+
import java.text.ParseException;
37+
import java.text.SimpleDateFormat;
3538
import java.util.Arrays;
3639
import java.util.Date;
3740
import java.util.Enumeration;
3841
import java.util.Map;
42+
import java.util.TimeZone;
3943
import java.util.zip.ZipEntry;
4044
import java.util.zip.ZipInputStream;
4145
import java.util.zip.ZipOutputStream;
46+
4247
import javax.annotation.Nonnull;
48+
4349
import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
4450
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
4551
import org.apache.commons.compress.archivers.zip.ZipExtraField;
@@ -889,18 +895,54 @@ public void testForcedFileModes()
889895
public void testFixedEntryModificationTime()
890896
throws IOException
891897
{
892-
final long almostMinDosTime = 315532802000L;
898+
final long almostMinDosTime = toLocalTimeZone( 315532802000L );
899+
893900
final File zipFile = getTestFile( "target/output/zip-with-fixed-entry-modification-times.zip" );
894901
final ZipArchiver archiver = getZipArchiver( zipFile );
895902
archiver.setLastModifiedDate( new Date( almostMinDosTime ) );
896903
archiver.addDirectory( new File( "src/test/resources/zip-timestamp" ) );
897904
archiver.createArchive();
898905

899906
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+
}
904946
}
905947

906948
}

0 commit comments

Comments
 (0)