Top Description Fields Constructors Methods
org.apache.commons.compress.archivers.zip

public abstract Class ZipUtil

extends Object
Class Inheritance
Imports
java.io.IOException, java.math.BigInteger, java.time.Instant, .LocalDateTime, .ZoneId, java.util.Arrays, .Calendar, .Date, java.util.zip.CRC32, .ZipEntry

Utility class for handling DOS and Java time conversions.

Field Summary

Modifier and TypeField and Description
private static final long
DOSTIME_BEFORE_1980

DOS time constant for representing timestamps before 1980.

private static final long
DOSTIME_BEFORE_1980_AS_JAVA_TIME

Java time representation of the smallest date/time ZIP can handle

private static final long
UPPER_DOSTIME_BOUND

Approximately 128 years, in milliseconds (ignoring leap years, etc.).

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

the unsigned int as a long.
adjustToLong
(final int
the value to treat as unsigned int.
i
)

Assumes a negative integer really is a positive integer that has wrapped around and re-creates the original value.

pack-priv static long

Returns:

long representation of the BigInteger.
bigToLong
(final BigInteger
BigInteger to convert.
big
)

Converts a BigInteger into a long, and blows up (NumberFormatException) if the BigInteger is too big.

pack-priv static boolean
canHandleEntryData(final ZipArchiveEntry entry)

Tests if this library is able to read or write the given entry.

pack-priv static void
checkRequestedFeatures(final ZipArchiveEntry ze)

Checks whether the entry requires features not (yet) supported by the library and throws an exception if it does.

pack-priv static byte[]
copy(final byte[] from)

Creates a copy of the given array - or return null if the argument is null.

pack-priv static void
copy(final byte[] from, final byte[] to, final int offset)

private static Date
dosToJavaDate(final long dosTime)

public static long

Returns:

converted time
dosToJavaTime
(final long
time to convert
dosTime
)

Converts DOS time to Java time (number of milliseconds since epoch).

public static Date

Returns:

a Date instance corresponding to the given time.
fromDosTime
(final ZipLong
contains the stored DOS time.
zipDosTime
)

Converts a DOS date/time field to a Date object.

private static String
getUnicodeStringIfOriginalMatches(final AbstractUnicodeExtraField f, final byte[] orig)

If the stored CRC matches the one of the given name, return the Unicode name of the given field.

public static boolean

Returns:

true if the time can be safely represented as DOS time, false otherwise
isDosTime
(final long
time in milliseconds since epoch
time
)

Tests whether a given time (in milliseconds since Epoch) can be safely represented as DOS time

private static LocalDateTime
javaEpochToLocalDateTime(final long time)

private static long
javaToDosTime(final long t)

pack-priv static BigInteger

Returns:

BigInteger representation of the provided long.
longToBig
(long
long to convert to BigInteger.
l
)

Converts a long into a BigInteger.

public static byte[]

Returns:

the reversed array (mutated in-place, but also returned for convenience).
reverse
(final byte[]
to reverse (mutated in-place, but also returned for convenience).
array
)

Reverses a byte[] array.

pack-priv static void
setNameAndCommentFromExtraFields(final ZipArchiveEntry ze, final byte[] originalNameBytes, final byte[] commentBytes)

If the entry has Unicode*ExtraFields and the CRCs of the names/comments match those of the extra fields, transfer the known Unicode values from the extra field.

public static int

Returns:

int representation of the provided byte
signedByteToUnsignedInt
(final byte
byte to convert to int
b
)

Converts a signed byte into an unsigned integer representation (e.g., -1 becomes 255).

private static boolean

Returns:

true if the entry isn't encrypted at all
supportsEncryptionOf
(final ZipArchiveEntry entry)

Tests if this library supports the encryption used by the given entry.

private static boolean

Returns:

true if the compression method is supported
supportsMethodOf
(final ZipArchiveEntry entry)

Tests if this library supports the compression method used by the given entry.

public static ZipLong

Returns:

the date as a ZipLong
toDosTime
(final Date
the Date to convert
time
)

Converts a Date object to a DOS date/time field.

public static byte[]

Returns:

the date as a byte array
toDosTime
(final long
number of milliseconds since the epoch
t
)

Converts a Date object to a DOS date/time field.

public static void
toDosTime(final long
number of milliseconds since the epoch
t
,
final byte[]
the output buffer
buf
,
final int
The offset within the output buffer of the first byte to be written. must be non-negative and no larger than buf.length-4
offset
)

Converts a Date object to a DOS date/time field.

public static byte

Returns:

byte representation of the provided int
unsignedIntToSignedByte
(final int
integer to convert to byte
i
)

Converts an unsigned integer to a signed byte (e.g., 255 becomes -1).

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DOSTIME_BEFORE_1980back to summary
private static final long DOSTIME_BEFORE_1980

DOS time constant for representing timestamps before 1980. Smallest date/time ZIP can handle.

MS-DOS records file dates and times as packed 16-bit values. An MS-DOS date has the following format.

Bits Contents

  • 0-4: Day of the month (1-31).
  • 5-8: Month (1 = January, 2 = February, and so on).
  • 9-15: Year offset from 1980 (add 1980 to get the actual year).
An MS-DOS time has the following format.

Bits Contents

  • 0-4: Second divided by 2.
  • 5-10: Minute (0-59).
  • 11-15: Hour (0-23 on a 24-hour clock).
This constant expresses the minimum DOS date of January 1st 1980 at 00:00:00 or, bit-by-bit:
  • Year: 0000000
  • Month: 0001
  • Day: 00001
  • Hour: 00000
  • Minute: 000000
  • Seconds: 00000

This was copied from ZipEntry.

Since
1.23
DOSTIME_BEFORE_1980_AS_JAVA_TIMEback to summary
private static final long DOSTIME_BEFORE_1980_AS_JAVA_TIME

Java time representation of the smallest date/time ZIP can handle

UPPER_DOSTIME_BOUNDback to summary
private static final long UPPER_DOSTIME_BOUND

Approximately 128 years, in milliseconds (ignoring leap years, etc.).

This establish an approximate high-bound value for DOS times in milliseconds since epoch, used to enable an efficient but sufficient bounds check to avoid generating extended last modified time entries.

Calculating the exact number is locale dependent, would require loading TimeZone data eagerly, and would make little practical sense. Since DOS times theoretically go to 2107 - with compatibility not guaranteed after 2099 - setting this to a time that is before but near 2099 should be sufficient.

This was copied from ZipEntry.

Since
1.23

Constructor Detail

ZipUtilback to summary
public ZipUtil()

Method Detail

adjustToLongback to summary
public static long adjustToLong(final int i)

Assumes a negative integer really is a positive integer that has wrapped around and re-creates the original value.

Parameters
i:int

the value to treat as unsigned int.

Returns:long

the unsigned int as a long.

bigToLongback to summary
pack-priv static long bigToLong(final BigInteger big)

Converts a BigInteger into a long, and blows up (NumberFormatException) if the BigInteger is too big.

Parameters
big:BigInteger

BigInteger to convert.

Returns:long

long representation of the BigInteger.

canHandleEntryDataback to summary
pack-priv static boolean canHandleEntryData(final ZipArchiveEntry entry)

Tests if this library is able to read or write the given entry.

checkRequestedFeaturesback to summary
pack-priv static void checkRequestedFeatures(final ZipArchiveEntry ze) throws UnsupportedZipFeatureException

Checks whether the entry requires features not (yet) supported by the library and throws an exception if it does.

copyback to summary
pack-priv static byte[] copy(final byte[] from)

Creates a copy of the given array - or return null if the argument is null.

copyback to summary
pack-priv static void copy(final byte[] from, final byte[] to, final int offset)
dosToJavaDateback to summary
private static Date dosToJavaDate(final long dosTime)
dosToJavaTimeback to summary
public static long dosToJavaTime(final long dosTime)

Converts DOS time to Java time (number of milliseconds since epoch).

Parameters
dosTime:long

time to convert

Returns:long

converted time

fromDosTimeback to summary
public static Date fromDosTime(final ZipLong zipDosTime)

Converts a DOS date/time field to a Date object.

Parameters
zipDosTime:ZipLong

contains the stored DOS time.

Returns:Date

a Date instance corresponding to the given time.

getUnicodeStringIfOriginalMatchesback to summary
private static String getUnicodeStringIfOriginalMatches(final AbstractUnicodeExtraField f, final byte[] orig)

If the stored CRC matches the one of the given name, return the Unicode name of the given field.

If the field is null or the CRCs don't match, return null instead.

isDosTimeback to summary
public static boolean isDosTime(final long time)

Tests whether a given time (in milliseconds since Epoch) can be safely represented as DOS time

Parameters
time:long

time in milliseconds since epoch

Returns:boolean

true if the time can be safely represented as DOS time, false otherwise

Since
1.23
javaEpochToLocalDateTimeback to summary
private static LocalDateTime javaEpochToLocalDateTime(final long time)
javaToDosTimeback to summary
private static long javaToDosTime(final long t)
longToBigback to summary
pack-priv static BigInteger longToBig(long l)

Converts a long into a BigInteger. Negative numbers between -1 and -2^31 are treated as unsigned 32 bit (e.g., positive) integers. Negative numbers below -2^31 cause an IllegalArgumentException to be thrown.

Parameters
l:long

long to convert to BigInteger.

Returns:BigInteger

BigInteger representation of the provided long.

reverseback to summary
public static byte[] reverse(final byte[] array)

Reverses a byte[] array. Reverses in-place (thus provided array is mutated), but also returns same for convenience.

Parameters
array:byte[]

to reverse (mutated in-place, but also returned for convenience).

Returns:byte[]

the reversed array (mutated in-place, but also returned for convenience).

Since
1.5
setNameAndCommentFromExtraFieldsback to summary
pack-priv static void setNameAndCommentFromExtraFields(final ZipArchiveEntry ze, final byte[] originalNameBytes, final byte[] commentBytes)

If the entry has Unicode*ExtraFields and the CRCs of the names/comments match those of the extra fields, transfer the known Unicode values from the extra field.

signedByteToUnsignedIntback to summary
public static int signedByteToUnsignedInt(final byte b)

Converts a signed byte into an unsigned integer representation (e.g., -1 becomes 255).

Parameters
b:byte

byte to convert to int

Returns:int

int representation of the provided byte

Since
1.5
supportsEncryptionOfback to summary
private static boolean supportsEncryptionOf(final ZipArchiveEntry entry)

Tests if this library supports the encryption used by the given entry.

Returns:boolean

true if the entry isn't encrypted at all

supportsMethodOfback to summary
private static boolean supportsMethodOf(final ZipArchiveEntry entry)

Tests if this library supports the compression method used by the given entry.

Returns:boolean

true if the compression method is supported

toDosTimeback to summary
public static ZipLong toDosTime(final Date time)

Converts a Date object to a DOS date/time field.

Parameters
time:Date

the Date to convert

Returns:ZipLong

the date as a ZipLong

toDosTimeback to summary
public static byte[] toDosTime(final long t)

Converts a Date object to a DOS date/time field.

Stolen from InfoZip's fileio.c

Parameters
t:long

number of milliseconds since the epoch

Returns:byte[]

the date as a byte array

toDosTimeback to summary
public static void toDosTime(final long t, final byte[] buf, final int offset)

Converts a Date object to a DOS date/time field.

Stolen from InfoZip's fileio.c

Parameters
t:long

number of milliseconds since the epoch

buf:byte[]

the output buffer

offset:int

The offset within the output buffer of the first byte to be written. must be non-negative and no larger than buf.length-4

unsignedIntToSignedByteback to summary
public static byte unsignedIntToSignedByte(final int i)

Converts an unsigned integer to a signed byte (e.g., 255 becomes -1).

Parameters
i:int

integer to convert to byte

Returns:byte

byte representation of the provided int

Exceptions
IllegalArgumentException:
if the provided integer is not inside the range [0,255].
Since
1.5