updating zipentry

This commit is contained in:
Chris Jordan 2013-07-08 13:55:00 -06:00
parent 055f820cac
commit 6970bb26ae

View File

@ -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; 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 * Class to store and retrieve information for entries in a zip file
* Contains variables for all standard zip format field as well as * Contains variables for all standard zip format field as well as
* setter and accessor methods * 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 { public class ZipEntry {
String name; String name;
//Minimum version needed to extract the file(s) from a compressed state short reqVersion = -1;
short reqVersion; short compressionMethod = -1;
int modTimeDate = -1;
//Method used to compress file long millisTime = -1;
short compressionMethod; int crc = -1;
int compSize = 0;
//Format of date and time are both 2 byte fields int uncompSize = 0;
short modTime; int offset = -1;
short modDate;
//CRC-32
int crc;
//Sizes of file
int compSize;
int uncompSize;
int offset;
public ZipEntry(String name) { public ZipEntry(String name) {
this.name = name; this.name = name;
setTimeDate(); setTime(System.currentTimeMillis());
compSize = 0;
uncompSize = 0;
crc = 0;
offset = 0;
} }
//Method to return name of the file //Method to return name of the file
@ -52,19 +61,69 @@ public class ZipEntry {
return getName().endsWith("/"); return getName().endsWith("/");
} }
//Method to return the compressed size of the file /**
public int getCompressedSize() { * Method setRequiredVersion:
return compSize; *
} * 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
//Method to return the uncompressed size of the file *
public int getSize() { * @author Christopher Jordan
return uncompSize; */
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(): * Method computeDOSDateTime():
* Creates a calendar object to retrieve date and time information *
* 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 * 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 * 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 * 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 DAY_OF_MONTH = 5;
final int HOUR_OF_DAY = 11; final int HOUR_OF_DAY = 11;
final int MINUTE = 12; final int MINUTE = 12;
@ -86,11 +146,13 @@ public class ZipEntry {
final int SECOND = 13; final int SECOND = 13;
final int YEAR = 1; final int YEAR = 1;
//Create calendar object, then set time to value passed in
Calendar modCalendar = Calendar.getInstance(); Calendar modCalendar = Calendar.getInstance();
modCalendar.setTime(new Date(currentTime));
//Hour //Hour
int timeBits = modCalendar.get(HOUR_OF_DAY); int timeBits = modCalendar.get(HOUR_OF_DAY);
timeBits = timeBits - 6; timeBits = timeBits - 6;
timeBits = timeBits << 6; timeBits = timeBits << 6;
//Minutes //Minutes
@ -100,18 +162,15 @@ public class ZipEntry {
//Seconds //Seconds
int secBits = 0x1f & (modCalendar.get(SECOND)); int secBits = 0x1f & (modCalendar.get(SECOND));
secBits = secBits >> 1; secBits = secBits >> 1; //Divide by 2
timeBits = timeBits ^ secBits; timeBits = timeBits ^ secBits;
//Store Time
modTime = (short)timeBits;
//Year //Year
int dateBits = (modCalendar.get(YEAR) -1980); int dateBits = (modCalendar.get(YEAR) -1980);
dateBits = dateBits << 4; dateBits = dateBits << 4;
//Month //Month
int month = 0xf & ((modCalendar.get(MONTH)) + 1); int month = 0xf & ((modCalendar.get(MONTH)) + 1);
dateBits = dateBits ^ month; dateBits = dateBits ^ month;
dateBits = dateBits << 5; dateBits = dateBits << 5;
@ -120,41 +179,33 @@ public class ZipEntry {
dateBits = dateBits ^ dayBits; dateBits = dateBits ^ dayBits;
//Store Date //Store Date
modDate = (short)dateBits; int storeDate = ((dateBits << 16) ^ (timeBits));
return storeDate;
} }
//Method to set the minimum version required to open the zip file //Methods to set and get the uncompressed size of the entry
//Valid values for the compression method are the numbers 1.0 to 10.0 public void setSize(int size){
public boolean setRequiredVersion(float versionFloat){ if (size < 0){
//Check for valid version numbers return;
if (versionFloat < 1 || versionFloat > 100){ }
return false; else
} uncompSize = size;
//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 public int getSize() {
//Valid values for the compression method are the numbers 0 to 19 and 99 return uncompSize;
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;
}
} }
} //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;
}
}