Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to unzip many Zip files

 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
bob morkos
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works now. I have to call my Unix Script Test instead of Test.sh. Thanks a lot.
 
shirish katti
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18276
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic