java.lang.Object
org.apache.commons.compress.archivers.zip.ZipUtil

public abstract class ZipUtil extends Object
Utility class for handling DOS and Java time conversions.
  • Field Details

    • DOSTIME_BEFORE_1980

      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
      See Also:
    • DOSTIME_BEFORE_1980_AS_JAVA_TIME

      private static final long DOSTIME_BEFORE_1980_AS_JAVA_TIME
      Java time representation of the smallest date/time ZIP can handle
    • UPPER_DOSTIME_BOUND

      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
      See Also:
  • Constructor Details

    • ZipUtil

      public ZipUtil()
  • Method Details

    • adjustToLong

      public static long adjustToLong(int i)
      Assumes a negative integer really is a positive integer that has wrapped around and re-creates the original value.
      Parameters:
      i - the value to treat as unsigned int.
      Returns:
      the unsigned int as a long.
    • canHandleEntryData

      static boolean canHandleEntryData(ZipArchiveEntry entry)
      Tests if this library is able to read or write the given entry.
    • checkRequestedFeatures

      static void checkRequestedFeatures(ZipArchiveEntry ze) throws UnsupportedZipFeatureException
      Checks whether the entry requires features not (yet) supported by the library and throws an exception if it does.
      Throws:
      UnsupportedZipFeatureException
    • copy

      static byte[] copy(byte[] from)
      Creates a copy of the given array - or return null if the argument is null.
    • copy

      static void copy(byte[] from, byte[] to, int offset)
    • dosToJavaDate

      private static Date dosToJavaDate(long dosTime)
    • dosToJavaTime

      public static long dosToJavaTime(long dosTime)
      Converts DOS time to Java time (number of milliseconds since epoch).
      Parameters:
      dosTime - time to convert
      Returns:
      converted time
    • fromDosTime

      public static Date fromDosTime(ZipLong zipDosTime)
      Converts a DOS date/time field to a Date object.
      Parameters:
      zipDosTime - contains the stored DOS time.
      Returns:
      a Date instance corresponding to the given time.
    • getUnicodeStringIfOriginalMatches

      private static String getUnicodeStringIfOriginalMatches(AbstractUnicodeExtraField field, byte[] originalNameBytes)
      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.

    • isDosTime

      public static boolean isDosTime(long time)
      Tests whether a given time (in milliseconds since Epoch) can be safely represented as DOS time
      Parameters:
      time - time in milliseconds since epoch
      Returns:
      true if the time can be safely represented as DOS time, false otherwise
      Since:
      1.23
    • javaEpochToLocalDateTime

      private static LocalDateTime javaEpochToLocalDateTime(long time)
    • javaToDosTime

      private static long javaToDosTime(long t)
    • longToBig

      static BigInteger longToBig(long l)

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

      Parameters:
      l - long to convert to BigInteger.
      Returns:
      BigInteger representation of the provided long.
    • newZipException

      static ZipException newZipException(String message, Throwable cause)
      Constructs a new ZipException.
      Parameters:
      message - the detail message.
      cause - throwable The cause of this Throwable.
      Returns:
      a new ZipException.
    • reverse

      public static byte[] reverse(byte[] array)
      Reverses a byte[] array. Reverses in-place (thus provided array is mutated), but also returns same for convenience.
      Parameters:
      array - to reverse (mutated in-place, but also returned for convenience).
      Returns:
      the reversed array (mutated in-place, but also returned for convenience).
      Since:
      1.5
    • setNameAndCommentFromExtraFields

      static void setNameAndCommentFromExtraFields(ZipArchiveEntry ze, byte[] originalNameBytes, 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.
    • signedByteToUnsignedInt

      @Deprecated public static int signedByteToUnsignedInt(byte b)
      Deprecated.
      Converts a signed byte into an unsigned integer representation (for example, -1 becomes 255).
      Parameters:
      b - byte to convert to int
      Returns:
      int representation of the provided byte
      Since:
      1.5
    • supportsEncryptionOf

      private static boolean supportsEncryptionOf(ZipArchiveEntry entry)
      Tests if this library supports the encryption used by the given entry.
      Returns:
      true if the entry isn't encrypted at all
    • supportsMethodOf

      private static boolean supportsMethodOf(ZipArchiveEntry entry)
      Tests if this library supports the compression method used by the given entry.
      Returns:
      true if the compression method is supported
    • toDosTime

      public static ZipLong toDosTime(Date time)
      Converts a Date object to a DOS date/time field.
      Parameters:
      time - the Date to convert
      Returns:
      the date as a ZipLong
    • toDosTime

      public static byte[] toDosTime(long t)
      Converts a Date object to a DOS date/time field.

      Stolen from InfoZip's fileio.c

      Parameters:
      t - number of milliseconds since the epoch
      Returns:
      the date as a byte array
    • toDosTime

      public static void toDosTime(long t, byte[] buf, int offset)
      Converts a Date object to a DOS date/time field.

      Stolen from InfoZip's fileio.c

      Parameters:
      t - number of milliseconds since the epoch
      buf - the output buffer
      offset - The offset within the output buffer of the first byte to be written. must be non-negative and no larger than buf.length-4
    • toLong

      static long toLong(BigInteger big)
      Converts a BigInteger to a long, and throws a NumberFormatException if the BigInteger is too big.
      Parameters:
      big - BigInteger to convert.
      Returns:
      BigInteger converted to a long.
    • unsignedIntToSignedByte

      public static byte unsignedIntToSignedByte(int i)
      Converts an unsigned integer to a signed byte (for example, 255 becomes -1).
      Parameters:
      i - integer to convert to byte
      Returns:
      byte representation of the provided int
      Throws:
      IllegalArgumentException - if the provided integer is not inside the range [0,255].
      Since:
      1.5