compressing and decompressing a string list
Hi All,
i need a help:
i have an string list say,
String sample="test";
String sample1="test1";
String sample2="test3";
i want to compress this sample,sample1,sample2 and store that compressed results into another string variable.
Ex:
compressedSample=sample;
compressedSample1=sample1;
compressedSample2=sample2;
similarly how can i uncompress the compressedSample,compressedSample1,compressedSample2 variables.
Thanks in advance
Could you explain your question further?
http://java.sun.com/developer/technicalArticles/Programming/compression/
Hi AnanSmriti,
i have string variables say string1,string2,string3 i want to compress these String variables(string1,string2,string3) using java compression and after the compression,once again i want to assign these compressed strings to some other variable.
for example:
String string1="test";
String compressedString="";
compress(string1)
{
return string1;
}
compressedstring=string1;
Try something like this :
Compressing :
ByteArrayOutputStream aos = new ByteArrayOutputStream();
GZIPOutputStream gos = new GZIPOutputStream(aos);
byte[] ba = "the_string_to_compress".getBytes();
gos.write(ba,0,ba.length);
gos.flush();
String compressed_string = aos.toString();
gos.close();
Decompressing :
GZIPInputStream zin = new GZIPInputStream( new ByteArrayInputStream( compressed_string.getBytes() ) );
StringBuffer dat = new StringBuffer();
byte[] buf = new Byte[128];
for(int r; (r=zin.read(buf))!=-1;) dat.append( new String(buf,0,r) );
zin.close();
String org_string = dat.toString();
Hi Puffhans,when i compress the string as per your suggeted method the size of the string increase higher than before it is compressed.can you help me how to reduce the size of the string?
I think the string you are compressing needs to have a certin size to shrink in the compression process.There is another error in the code i posted it does not work !Insted have a look at this : http://www.exampledepot.com/egs/java.util.zip/pkg.html
Hi Puffhans, In that example also it exceeds the mormal string size.i use compress byte array class.how can i control the size?please help me.
Well your string is proberly to short and to unique to be compressed.Try compressing :"xxxxxxxxxxxxxxxxxxxxxxxxxx" ( x100 )vs"qwertyuiopasdfghjklzxcvbnm" ( x100 )
Puffhans,I cant get you.could you explain in a easy way.
> i cant get you.could you explain in a easy way.
Well your string is proberly to short and to unique to be compressed.
what he/she mean was:
Well, your string is probably too short and too unique to be compressed.
do your test with a longger string. (maybe more than 1MB).
Hi,i am compressing the following strings String string1=444043399, string2=444043399 string3=070601 string4=050108string5=HRDCELIdoes the compression thing works for these strings?
> String string1=444043399,
what you mean?
does the compression thing works for these strings?
for this string?
String string1="444043399";
the compression will works, but the result will not satisfy you.
you don't supply a good environtment for your test. it's like you want to test a F1 car in only 1 meters lap. after you test that super car, you will say "so engine F1 is not fast than my leg".
hi shadinata, I understand your points,thanks.i want to compress this type of string having low bytes.is there any idea?
> hi shadinata,
> I understand your
> points,thanks.i want to compress this type of string
> having low bytes.is there any idea?
You seem to be missing the point. Compression is normally achieved by replacing frequently occurring substrings by a small place holder. To do this one needs to provide an index so that the reverse operation can be performed. In small strings the index may be larger than the original string so no compression can take place.
If you have only small amounts of data to start with when why on earth would you want to buggerise around compressing it?... if you compress it and it comes out five bytes larger than the orginal then who really gives a toss...
Lets put it into perspective... I've lost all of five bytes on a machine with 2,147,483,648 bytes of RAM, two 85,899,345,920 hard disks, and a 471,859,200 bytes per hour internet connection.
Compression is really good for really big files. It's also really good at storing lots & lots of little files, because the minimum addressable block under XP on that 80Gig harddisk is 1k (I think).... so 1 byte file takes actually consume 1024 bytes of storage.
Don't bother compressing "xxxxxxxxxxxxxx" it's just a waste of brain waves writing the code to do so, and an utter waste of time for those who have to maintain it.
Keith.
Message was edited by: corlettk
> hi shadinata,> I understand your points,thanks.i want to compress this type of string > having low bytes.is there any idea?i have no idea why you need that since you take more cost to compress-decompress your tiny string than use it directly.
Hi All, Thanks for your points.i understand the compression concept.