Solved

Unzip passing a StringBuffer and returning a StringBuffer

Posted on 2006-06-12
9
661 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:ryno71
Comment Utility
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
Comment Utility
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
Comment Utility
InputStream in = new InflaterInputStream(new ByteArrayInputStream(rd));

(where 'rd' is byte[])
0
 

Author Comment

by:ryno71
Comment Utility
Thanks CEHJ

Thanks for the input and help!

ryno71
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

10 Experts available now in Live!

Get 1:1 Help Now