From 6970bb26ae429b870bfe57648bbba7743d9e8f6b Mon Sep 17 00:00:00 2001 From: Chris Jordan Date: Mon, 8 Jul 2013 13:55:00 -0600 Subject: [PATCH] updating zipentry --- classpath/java/util/zip/ZipEntry.java | 215 ++++++++++++++++---------- 1 file changed, 133 insertions(+), 82 deletions(-) diff --git a/classpath/java/util/zip/ZipEntry.java b/classpath/java/util/zip/ZipEntry.java index b396127187..d92e9f8113 100644 --- a/classpath/java/util/zip/ZipEntry.java +++ b/classpath/java/util/zip/ZipEntry.java @@ -1,45 +1,54 @@ +/* Copyright (c) 2008-2013, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + package java.util.zip; -import java.util.Calendar; -import java.util.Date; - /** - * class ZipEntry: + * Class ZipEntry: * * Class to store and retrieve information for entries in a zip file * Contains variables for all standard zip format field as well as * setter and accessor methods * - * @author ReadyTalk Summer 2013 Intern Team - **/ + * "name" is used to store the string name of the entrys + * "reqVersion" stores a byte encoded minimum required version to open the zip + * "compressionMethod" stores the method used to compress the zip + * "modTimeDate" stores an MSDOS time field "millisTime" stores time in long format + * "crc" stores crc data for the zip entry + * "compSize" and "uncompSize" store compressed and uncompressed sizes + * "offset" stores data regarding the offset from the start of the zip file + * + * @author Christopher Jordan + * @author David Chau + * @author Aaron Davis + * @author Riley Moses + */ + +import java.util.Calendar; +import java.util.Date; + + public class ZipEntry { String name; - //Minimum version needed to extract the file(s) from a compressed state - short reqVersion; - - //Method used to compress file - short compressionMethod; - - //Format of date and time are both 2 byte fields - short modTime; - short modDate; - - //CRC-32 - int crc; - - //Sizes of file - int compSize; - int uncompSize; - - int offset; + short reqVersion = -1; + short compressionMethod = -1; + int modTimeDate = -1; + long millisTime = -1; + int crc = -1; + int compSize = 0; + int uncompSize = 0; + int offset = -1; public ZipEntry(String name) { this.name = name; - setTimeDate(); - compSize = 0; - uncompSize = 0; - crc = 0; - offset = 0; + setTime(System.currentTimeMillis()); } //Method to return name of the file @@ -52,19 +61,69 @@ public class ZipEntry { return getName().endsWith("/"); } - //Method to return the compressed size of the file - public int getCompressedSize() { - return compSize; - } - - //Method to return the uncompressed size of the file - public int getSize() { - return uncompSize; + /** + * Method setRequiredVersion: + * + * Method to set the minimum version required to open the zip file + * Valid values for the compression method are the numbers 1.0 to 10.0 + * + * @author Christopher Jordan + */ + private boolean setRequiredVersion(float versionFloat){ + //Check for valid version numbers + if (versionFloat < 1 || versionFloat > 100){ + return false; + } + + //Convert to short value for storage + versionFloat = versionFloat * 10; + short versionShort = (short)versionFloat; + + //Set value of version + reqVersion = versionShort; + return true; } + //Method to set the compression method for the file + //Valid methods are "stored" = 0 or "deflated" = 8 + public void setMethod(short compMethod){ + if (compMethod == 0 || compMethod == 8){ + this.compressionMethod = compMethod; + } + } + + public int getMethod(){ + return this.compressionMethod; + } + + //Methods to set and get the crc for the entry + public void setCrc(int crc){ + if (crc < 0 || crc > 0xffffffff){ + return; + } + else + this.crc = crc; + } + + public int getCrc(){ + return this.crc; + } + + //Methods to set and get the time and date + public void setTime(long currentTime){ + modTimeDate = computeDOSDateTime(currentTime); + millisTime = currentTime; + } + + public long getTime(){ + return millisTime; + + } + /** - * Method setTime Date(): - * Creates a calendar object to retrieve date and time information + * Method computeDOSDateTime(): + * + * Takes the time from a long and converts to a Calendar object * * Time is stored in the MSDOS format 5 bits for hour, 6 bits for min, 5 bits for seconds * Hours are in military time and must be adjusted to time zone @@ -76,9 +135,10 @@ public class ZipEntry { * * Bit masks and shifting are used to build the time and date bytes * - * @author cjordan + * @author Christopher Jordan + * @author Aaron Davis **/ - public void setTimeDate(){ + private static int computeDOSDateTime(long currentTime){ final int DAY_OF_MONTH = 5; final int HOUR_OF_DAY = 11; final int MINUTE = 12; @@ -86,11 +146,13 @@ public class ZipEntry { final int SECOND = 13; final int YEAR = 1; + //Create calendar object, then set time to value passed in Calendar modCalendar = Calendar.getInstance(); + modCalendar.setTime(new Date(currentTime)); //Hour int timeBits = modCalendar.get(HOUR_OF_DAY); - timeBits = timeBits - 6; + timeBits = timeBits - 6; timeBits = timeBits << 6; //Minutes @@ -100,18 +162,15 @@ public class ZipEntry { //Seconds int secBits = 0x1f & (modCalendar.get(SECOND)); - secBits = secBits >> 1; + secBits = secBits >> 1; //Divide by 2 timeBits = timeBits ^ secBits; - //Store Time - modTime = (short)timeBits; - //Year int dateBits = (modCalendar.get(YEAR) -1980); dateBits = dateBits << 4; //Month - int month = 0xf & ((modCalendar.get(MONTH)) + 1); + int month = 0xf & ((modCalendar.get(MONTH)) + 1); dateBits = dateBits ^ month; dateBits = dateBits << 5; @@ -120,41 +179,33 @@ public class ZipEntry { dateBits = dateBits ^ dayBits; //Store Date - modDate = (short)dateBits; - + int storeDate = ((dateBits << 16) ^ (timeBits)); + return storeDate; } - - //Method to set the minimum version required to open the zip file - //Valid values for the compression method are the numbers 1.0 to 10.0 - public boolean setRequiredVersion(float versionFloat){ - //Check for valid version numbers - if (versionFloat < 1 || versionFloat > 100){ - return false; - } - - //Convert to short value for storage - versionFloat = versionFloat * 10; - short versionShort = (short)versionFloat; - - //Set value of version - reqVersion = versionShort; - return true; + + //Methods to set and get the uncompressed size of the entry + public void setSize(int size){ + if (size < 0){ + return; + } + else + uncompSize = size; } - - //Method to set the compression method for the file - //Valid values for the compression method are the numbers 0 to 19 and 99 - public boolean setCompressionMethod(short compMethod){ - if (compMethod == 99){ - compressionMethod = compMethod; - return true; - } - else if (compMethod < 0 || compMethod > 19){ - return false; - } - else{ - compressionMethod = compMethod; - return true; - } + + public int getSize() { + return uncompSize; } - -} \ No newline at end of file + + //Methods to set and get the compressed size of the entry + public void setCompressedSize(int size){ + if (size < 0){ + return; + } + else + compSize = size; + } + + public int getCompressedSize() { + return compSize; + } +}