Compress the image in sqlserver into a zip package

Source: Internet
Author: User

// 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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.