Solved

Unzip passing a StringBuffer and returning a StringBuffer

Posted on 2006-06-12
9
664 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

810 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