Solved

Unzip passing a StringBuffer and returning a StringBuffer

Posted on 2006-06-12
9
671 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

623 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