aspose file tools*
The moose likes Linux / UNIX and the fly likes Is it possible to unzip many Zip files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Is it possible to unzip many Zip files " Watch "Is it possible to unzip many Zip files " New topic
Author

Is it possible to unzip many Zip files

bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
I have many Zip files which the name differ from one to another. What I need is a command line that executes all zip files at once.
Like:
$Unzip *.zip
The above command does not work. Is there a command line that does this Unzipping functionality.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

You can write a loop to do this:

I don't know zip well enough to know if zip will do this on it's own.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
I wrote a java application that does this Unzipping Loop. My code is the following (It does the job, but how do I add this program in my Unix script?)
<UnzippApp.java>
public class UnzipApp {
/** Unzipp a Zip */
public UnzipApp() {

}
public static void main(String args[]) throws IOException {

String inDirectory="Zip/";
String outDirectory="images/";

String[] filenames;
String[] files=null;
File file = new File(inDirectory);
filenames = file.list();

for(int i=0; i< filenames.length; i++) { //Don't forget this!!
File fs= new File(file.getName() + File.separator + filenames[i]);
//System.out.println("File Name: " + fs);
System.out.println("Get Zip File: " + filenames[i]);
ZipFile f=new ZipFile(inDirectory + "" + filenames[i]);
//fs.delete();
Enumeration entries= f.entries();
System.out.println("Decompressing Data");

while(entries.hasMoreElements()){

ZipEntry entry=(ZipEntry) entries.nextElement();

InputStream in = f.getInputStream(entry);
FileOutputStream out = new FileOutputStream(outDirectory +
"" + entry.getName());

for(int ch=in.read();ch!=-1;ch=in.read()) out.write(ch);
out.close();
in.close();
} //end while
f.close();
} // end for
try {
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("Unzipping Completed Successfully.");
} //end main
} //end class
</UnzippApp.java>
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Oh, right! Using Java...what a novel idea.
Here's what I would do next: write a manifest file for the class that declares the class is 'executable.' Then bundle the manifest and class into a JAR so you could invoke:
$ java -jar sumpn.jar
To do this, your manifest file would look like this:

then just include that command in your script.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
You just lost me right there. Do you have an example or something. I don't know how to call my script from java. I did something like this and it does not work. I'm using the java.lang.Runnable to do this functionality.
bob morkos
Ranch Hand

Joined: Sep 06, 2001
Posts: 104
It works now. I have to call my Unix Script Test instead of Test.sh. Thanks a lot.
shirish katti
Ranch Hand

Joined: Nov 09, 2009
Posts: 34
Hi Bob, I am trying with the same code you posted to unzip a file.. But its not working its throwing an exception as too many entries in zip file can you please help me..
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Here's a small tweak to Michael Ernest's suggestion:



That way it won't try and unzip files that aren't zipfiles!

As far as I know, unzip only applies itself to one zipfile per execution. The Unix philosophy is not to load programs down with features if you can get the same effect by stringing simpler programs together as building blocks. Besides, the "globbing" feature is done at the shell level, not within Unix apps, which has implications for its command-line parser and hence the command syntax.


But its not working its throwing an exception as too many entries in zip file can you please help me..


This sounds like maybe one of the zipfiles is too complex. Try unzipping it manually and see. You may need a different unzipping utility program. I had an issue like that where one of the files in the ZIP exceeded 2GB (it was a VM disk image), so I had to use 7zip instead of unzip.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
 
subject: Is it possible to unzip many Zip files