Solved

ZipEntry, zipinputstream and default constructors.

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
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 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…

708 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

18 Experts available now in Live!

Get 1:1 Help Now