Solved

ZipEntry, zipinputstream and default constructors.

Posted on 2001-08-08
5
437 Views
Last Modified: 2012-06-21
Wanted to use Java to decompress(unzip) some files (but not all) from a given zip, I started playing with code samples and got an enumeration of a zipfile object to list all the files in a given listbox.  --- NO PROBLEM!

Then the idea was double click on the given file which needs decompressing and get that particular file to be decompressed.

Ofcourse I cant use the enumeration object because that could be pointing anywhere (so to speak) so what I treied was something like ...
ZipInputStream zipin = new ZipInputStream(new FileInputStream(name_of_file));
BufferedInputStream bis = new BufferedInputStream(zipin);
FileOutputStream fos = new FileOutputStream(s);
ze =  zipin.createZipEntry(specific_entry_in_file);

and err that's where the first problem reared its ugly head - createZipEntry is a protected function to use it I need to create an extended class built on ZipInputStream and create a public function which calls createZipEntry,, problem is I dont know how to handle the constructor which would pass the right InputStream. All my attempts have ended in compiler errors. Default constructors dont seem to be called by super,, but I could be wrong.

So this question comes in 2 parts, how do I handle the constructors etc in an extended class and has anyone experience in java unzip specifc files from an archive.

Many thanks
Matlock.


0
Comment
Question by:Matlock
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6367071
Maintain a hash of (name, zip entries) when you're getting a list of files in the zip file.
Then to get a stream for a specific file name use:

ZipEntry = (ZipEntry) hash.get(name);
InputStream in = zipfile.getInputStream(entry);

0
 

Author Comment

by:Matlock
ID: 6367425
OK, I'll accept this comment, the hashtable idea worked that got me out of that problem but I really could do with understanding the second bit of my problem namely how to handle the default constructor and [assing to it an input stream ( thats foxed me) - if it needs to be answered by someone-else then i can open it as a new question.
0
 
LVL 92

Expert Comment

by:objects
ID: 6367490
sorry I missed that bit.

Is this what you're after?

public MyZipInputStream extends ZipInputStream
{
   MyZipInputStream(InputStream in)
   {
      super(in);
   }
}

But it won't help cause you can't redefine method visibility.

0
 

Author Comment

by:Matlock
ID: 6367507
Thanks objects,
that did answer my question and helped me 2 ways,
1) a quick rethink of program flow
2) now know how to use with changing the default constructor

basicly for others what I now do is add entries to the hash table thus
while(alist.hasMoreElements()){
                                  Object obj = (ZipEntry) alist.nextElement();
                                String z = obj.toString();
                                ht.put(z,obj);
                                model.addElement(z);
                                 }

and when it comes to decompress

try {
                 FileOutputStream fos = new FileOutputStream(s);
                 ze = new ZipEntry((ZipEntry) ht.get(s));
                  int sz = (int) ze.getSize();
                  BufferedInputStream bis = new BufferedInputStream(zipfile.getInputStream(ze));
                  //JOptionPane.showMessageDialog(null,"Size of file is " + sz);
                 final int N = 1024;
                 byte buf[] = new byte[N];
                 int ln = 0;
                 while (sz > 0 &&  // workaround for bug
                              (ln = bis.read(buf, 0, Math.min(N, sz))) != -1) {
                                     fos.write(buf, 0, ln);
                               sz -= ln;
                                 //JOptionPane.showMessageDialog(null,"Buffer reads: " + buf);
                                 }
                    bis.close();
                    fos.flush();
                    fos.close();//http://www.cdrom.com/pub/infozip/zlib/rfc-deflate.html
                      }catch(IOException e) {}
using suns work around for a bug in the stream

Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 6367529
Looks about right :-)
Although ZipEntry has some useful information such as filename, directory name etc.

Thanks for the points :)

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
Error trying to install RTMT Win7 5 60
Groovy problem when using SOAPUI : DispatchException occurred 7 45
expectj telnet failing 5 36
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

777 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