Hi,
> Can someone suggest me an efficient way to convert a zip string to a
> normal string?
>
> We transfer a file via web service. We also zip the file. On the client
> side, we want to convert the zipped string to normal string again. What
> is the most efficient way?
Ehm, what exactly are you zipping - a File or a String? How do you get
the "content" to be unzipped (byte[], String, InputStream, ...)?
Anyway: Unzipping can be done with java.util.zip.ZipFile/ZipInputStream.
If you really have a String you want to (un-)zip, perhaps
java.io.StringReader/StringWriter might be interesting for you (although
I could imagine you need to think about encodings, because a String is
based on a char[], not on a byte[]).
In accordance to your first question: Note that this might not be the
most "efficient" way, although I think that you do not need to care
about some nanoseconds (for which you could optimise) in your webapp.
Hth,
Ingo
hftmit@gmail.com - 10 Aug 2006 18:28 GMT
Below is my current code:
passed in "zip" is a zipped Base64 encoded string. I guess if the
string is very long(it will be since it is zipped), there will be many
gabage string created in the middle. The solution you suggested seems
doesn't eliminate this. right?
public static String zipToString(String zip) throws Exception{
ByteArrayInputStream in = new
ByteArrayInputStream(Base64.decode(zip));
ZipInputStream zipIn = new ZipInputStream(in);
zipIn.getNextEntry();
byte[] buffer = new byte[512];
int len;
StringBuffer sb_result = new StringBuffer();
while ((len = zipIn.read(buffer)) > 0) {
sb_result.append(new String(buffer,0,len));
}
zipIn.closeEntry();
zipIn.close();
String result = sb_result.toString();
return result;
}
> Hi,
>
[quoted text clipped - 20 lines]
> Hth,
> Ingo
Ingo R. Homann - 11 Aug 2006 08:15 GMT
Hi,
> Below is my current code:
> passed in "zip" is a zipped Base64 encoded string. I guess if the
> string is very long(it will be since it is zipped),
Which means? 1MB? 10MB? 100MB? 1GB?
> there will be many
> gabage string created in the middle. The solution you suggested seems
> doesn't eliminate this. right?
Right. Of course you can reduce the problem by increasing the
buffer-size. But IMHO, the important question is: Why do you care? The
garbage will be collected - and it has just a short lifecycle.
Do you really encounter memory-problems? (Do you have problems if you
start your application with -Xmx500MB? With -Xmx200MB? ...?)
Not that you misunderstand me: It is always good to try to realize
potential problems - but on the other hand (when you think about
optimizations) it is always good to follow the rule "measure, not guess".
Ciao,
Ingo