Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

convert a zip string to a normal string

Thread view: 
hftmit@gmail.com - 10 Aug 2006 03:22 GMT
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?

thanks
Ingo R. Homann - 10 Aug 2006 13:56 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.