Solved

ZipEntry, zipinputstream and default constructors.

Posted on 2001-08-08
5
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 71
Why doesn't this text field show up on my Applet frame? 2 43
jmeter usage 4 64
Java Inheritance super keyword use 8 72
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

739 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