Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unzip passing a StringBuffer and returning a StringBuffer

Posted on 2006-06-12
9
Medium Priority
?
673 Views
Last Modified: 2012-08-13
Guys  I was thinking of doing the followign below...

Is there a better way?  Dont want to pass a normal string in as it may be a huge chuck of data.

public StringBuffer decompress(StringBuffer rd)throws FileNotFoundException,IOException
      {
            int resultcode = 0;
            StringBuffer data=null;
            data=rd;
            
            try{
            String str,strFileName;
           
            //OutPath = Extract to...
           
            FileInputStream inStream = new FileInputStream(rd.toString());            
                     
            ZipInputStream zipinputstream = new ZipInputStream(inStream);
           
            while (true) {    
                    ZipEntry zipentry = zipinputstream.getNextEntry();

                    if (zipentry == null)
                               break;                      
                    str = zipentry.getName();
                                         
                    File file = new File(str);
                    strFileName = file.getName();                        
           
                    int x = inStream.available();
                    byte [] rgb = new byte [x];                              
                    int n=zipinputstream.read(rgb);
                   
                    StringBuffer od = new StringBuffer(zipinputstream.available());
                   
                   
                    while (n > -1) {      
                                
                                String converted = new String(rgb, 0, n);
                                od.append(converted);
                                n=zipinputstream.read(rgb);
                    }
                    zipinputstream.closeEntry();
                       
                 
            }
           
            zipinputstream.close();
            inStream.close();                
   
            }
            catch(FileNotFoundException fex){}
            
            catch(IOException iex){}
            return data;

thanks
ryno71
0
Comment
Question by:ryno71
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16888173
>>FileInputStream inStream = new FileInputStream(rd.toString());      

would only work if the StringBuffer contains a file name. What is it you want to do?
0
 

Author Comment

by:ryno71
ID: 16888236
The string buffer has zipped data I placed in it, I want to upcompress the data and pass it back as a stringbuffer that has uncompressed data in it
0
 

Author Comment

by:ryno71
ID: 16888303
public StringBuffer decompress(StringBuffer rd)throws FileNotFoundException,IOException
      {
            int resultcode = 0;
            StringBuffer data=null;
            data=rd;
            
            try{
            String str,strFileName;
           
            //OutPath = Extract to...
           

            //still needs a file here.....
            FileInputStream inStream = new FileInputStream(rd.toString());            
                       
            ZipInputStream zipinputstream = new ZipInputStream(inStream);
           
             
                   

                                           
                   
                                         
                                           
           
                    int x = inStream.available();
                    byte [] rgb = new byte [x];                              
                    int n=zipinputstream.read(rgb);
                   
                    StringBuffer od = new StringBuffer(zipinputstream.available());
                   
                   
                    while (n > -1) {      
                                
                                String converted = new String(rgb, 0, n);
                                od.append(converted);
                                n=zipinputstream.read(rgb);
                    }
                    zipinputstream.closeEntry();
                       
                 
           
           
            zipinputstream.close();
            inStream.close();                
   
            }
            catch(FileNotFoundException fex){}
            
            catch(IOException iex){}
            return data;
      }
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 16888457
StringBuffer is not appropriate for doing this. StringBuffer is to hold text data. Binary data, whatever the source, should be held in a byte[]. You could do something similar, with the following signature:

public byte[] decompress(byte[] rd) throws IOException
0
 

Author Comment

by:ryno71
ID: 16888759
I probably should just change the Stringbuffer before I pass it to a byte array then I can take out the

FileInputStream inStream = new FileInputStream(rd.toString());    

so I would do this

byte[] ry=rd.toString().getBytes();

and pass the byte array...  then I just need to convert it to an InputStream
0
 

Author Comment

by:ryno71
ID: 16888769
oops just noticed your response  :)...  I still woul dneed to convert it from a byte [] to a inputstream
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 16888840
InputStream in = new InflaterInputStream(new ByteArrayInputStream(rd));

(where 'rd' is byte[])
0
 

Author Comment

by:ryno71
ID: 16888898
Thanks CEHJ

Thanks for the input and help!

ryno71
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16888920
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question