Solved

ZipEntry, zipinputstream and default constructors.

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 50
jboss 7.1 start up error 1 60
Eclipse for Java EE development 2 43
project copy paste issue 1 62
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

740 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