Solved

ZipEntry, zipinputstream and default constructors.

Posted on 2001-08-08
5
438 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 83
difference of if loops 23 62
tomcat startup error 5 102
Java Restore security prompts not working 10 56
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

856 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