• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Java --- ZIP & UNZIP

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can Java zip & unzip the any files(also cab files)
Thanks
Angela
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
It's not quite clear from your question whether you mean the classes in the java.util.jar and java.util.zip packages, or the jar tool that comes with the JDK.
Could you please elaborate?
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If your questions pertains to opening, reading and closing a zip file, then you could use "java.util.zip.ZipFile".

------------------
Nitin S
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following demonstrates reading from a ZIP file, listing all the files in the zip and displaying the contents of the first file in the zip. JAR file reading is similar, just with different classes and having a manifest.


source jguru.com - IO section
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code :

import java.io.*;
import java.util.Vector;
import java.util.zip.*;
class test
{
public static void ZipFile(String cfgFile) {
ZipOutputStream a_ZipOutStream = null;
File outFilename = new File(cfgFile);
int len;
try{
// Create a zip output stream
a_ZipOutStream = new ZipOutputStream( new FileOutputStream(outFilename.getName()+".zip"));
byte[] buf = new byte[1024];
File oneFile = new File(cfgFile);
if(oneFile.isFile()){
ZipEntry ze = new ZipEntry(cfgFile);
FileInputStream is = new FileInputStream(oneFile);
len = is.read(buf);
a_ZipOutStream.putNextEntry(ze);
ze.setTime(oneFile.lastModified());
a_ZipOutStream.write(buf,0,len);
a_ZipOutStream.closeEntry();
System.out.println(ze.getName() +" (" + ze.getCompressedSize()*100/ze.getSize() +"%)");
is.close();
}
}
catch(IOException ioe)
{
}
finally
{
if(a_ZipOutStream != null)
{
try{
a_ZipOutStream.close();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
a_ZipOutStream = null;
}
}
}

public static void main (String agrs[])
{
ZipFile("test.java");
}
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to pack and unpack what you do with WinZip with Java, you can cpmfortably do it with java.util.jar and java.util.zip packages, or with Jar.exe. As jar and zip use the sasme algorithm for compression, there will be no difference between .jar and .zip files.
 
You didn't tell me he was so big. Unlike this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic