Solved

Unzip passing a StringBuffer and returning a StringBuffer

Posted on 2006-06-12
9
662 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now