// Package ..
Public static void ziptest (){
// Obtain the image set. The thphoto object is used to save the image information.
List <tbphoto> List = D. returnallphoto ();
System. Out. println (list. Size ());
Try {
Fileoutputstream F = new fileoutputstream ("D: \ image .zip ");
Zipoutputstream out = new zipoutputstream (New dataoutputstream (f ));
Int I = 0;
For (tbphoto photo: List ){
// System. Out. println ("starts to compress the image... ");
// Photo. getcontent is read from the database as an image byte array. Byte []
Bytearrayinputstream instream = new bytearrayinputstream (photo
. Getcontent ());
Datainputstream in = new datainputstream (instream );
Out. putnextentry (New zipentry (I + "."
+ Gettypebystream (photo. getcontent ())));
Int C;
While (C = in. Read ())! =-1)
Out. Write (C );
In. Close ();
I ++;
}
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
/**
* Truncates the first 4 digits of the byte array and converts it to a hexadecimal string.
* @ Param SRC
* @ Return
*/
Public static string bytestohexstring (byte [] SRC ){
Stringbuilder = new stringbuilder ();
If (src = NULL | SRC. Length <4 ){
Return NULL;
}
For (INT I = 0; I <4; I ++ ){
Int v = SRC [I] & 0xff;
String HV = integer. tohexstring (v );
If (HV. Length () <2 ){
Stringbuilder. append (0 );
}
Stringbuilder. append (HV );
}
Return stringbuilder. tostring ();
}
/**
* Obtain the image type
* @ Param B
* @ Return
*/
Public static string gettypebystream (byte [] B ){
String type = bytestohexstring (B). touppercase ();
If (type. Contains ("ffd8ff ")){
Return "jpg ";
} Else if (type. Contains ("89504e47 ")){
Return "PNG ";
} Else if (type. Container ("47494638 ")){
Return "GIF ";
} Else if (type. Contains ("49347a00 ")){
Return "TIF ";
} Else if (type. Contains ("your d ")){
Return "BMP ";
}
Return type;
}