KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > zip > ZipFile

java.util.zip
Class ZipFile

java.lang.Object
  extended by java.util.zip.ZipFile
Direct Known Subclasses:
JarFile
See Also:
Top Examples, Source Code, NullPointerException

public static final int CENATT
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENATX
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENCOM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENCRC
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENDSK
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENEXT
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENFLG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENHDR
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENHOW
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENLEN
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENNAM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENOFF
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final long CENSIG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENSIZ
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENTIM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENVEM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int CENVER
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public void close()
           throws IOException
See Also:
getInputStream
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[170]A simple unzip utility
By Anonymous on 2004/02/26 15:37:36  Rate
//The following program demonstrates how to use the ZipFile class to create a simple unzip utility. It simply cycles through all of the entries in the file and copies each entry to a new file by reading its input stream. If an entry is a directory, it is created. The example is not robust and is only intended to demonstrate the ZipFile class. Obvious additions would include checking if a file already exists before creating it, prompting the user if it should be overwritten, as well as other error checking.  
  
  
 import java.io.*; 
 import java.util.*; 
 import java.util.zip.*; 
  
  
  
 public class Unzip  {  
  
  
   public static final void copyInputStream ( InputStream in, OutputStream out )  
   throws IOException 
    {  
     byte [  ]  buffer = new byte [ 1024 ] ; 
     int len; 
  
  
     while (  ( len = in.read ( buffer )  )   > = 0 )  
       out.write ( buffer, 0, len ) ; 
  
  
     in.close (  ) ; 
     out.close (  ) ; 
    }  
  
  
   public static final void main ( String [  ]  args )   {  
     Enumeration entries; 
     ZipFile zipFile; 
  
  
     if ( args.length != 1 )   {  
       System.err.println ( "Usage: Unzip zipfile" ) ; 
       return; 
      }  
  
  
     try  {  
       zipFile = new ZipFile ( args [ 0 ]  ) ; 
  
  
       entries = zipFile.entries (  ) ; 
  
  
       while ( entries.hasMoreElements (  )  )   {  
         ZipEntry entry =  ( ZipEntry ) entries.nextElement (  ) ; 
  
  
         if ( entry.isDirectory (  )  )   {  
           // Assume directories are stored parents first then children. 
           System.err.println ( "Extracting directory: " + entry.getName (  )  ) ; 
           // This is not robust, just for demonstration purposes. 
            ( new File ( entry.getName (  )  )  ) .mkdir (  ) ; 
           continue; 
          }  
  
  
         System.err.println ( "Extracting file: " + entry.getName (  )  ) ; 
         copyInputStream ( zipFile.getInputStream ( entry ) , 
            new BufferedOutputStream ( new FileOutputStream ( entry.getName (  )  )  )  ) ; 
        }  
  
  
       zipFile.close (  ) ; 
      }  catch  ( IOException ioe )   {  
       System.err.println ( "Unhandled exception:" ) ; 
       ioe.printStackTrace (  ) ; 
       return; 
      }  
    }  
  
  
  }  
  
  
 


public static final int ENDCOM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int ENDHDR
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int ENDOFF
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final long ENDSIG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int ENDSIZ
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int ENDSUB
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int ENDTOT
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public Enumeration<? extends ZipEntry> entries()
See Also:
IllegalStateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int EXTCRC
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int EXTHDR
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int EXTLEN
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final long EXTSIG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int EXTSIZ
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


protected void finalize()
                 throws IOException
See Also:
close(), Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ZipEntry getEntry(String name)
See Also:
IllegalStateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public InputStream getInputStream(ZipEntry entry)
                           throws IOException
See Also:
IllegalStateException, ZipException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getName()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCCRC
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCEXT
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCFLG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCHDR
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCHOW
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCLEN
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCNAM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final long LOCSIG
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCSIZ
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCTIM
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int LOCVER
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int OPEN_DELETE
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final int OPEN_READ
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int size()
See Also:
IllegalStateException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1836]ZipOutputStream, ZipFile and Linux unzip do not agree on the file count in zip
By Anonymous on 2006/10/26 15:10:03  Rate
I think there is a bug in ZipFile... 
  
  
 I use ZipOutputStream to build a zip file. The program zipped 72417 files, but after checking it with ZipFile, ZipFile.size (  )  reported only 6881 files in the file. So unfortunately, Java is unable to read all the entries in the zip file, but Linux's PK-INFO  ( unzip )  is able to read the file. 
  
  
 I went on an older UNIX  ( non linux )  server around here and tried unzip, it said: note: didn't find end-of-central-dir signature at end of central dir.  ( please check that you have transferred or created the zipfile in the appropriate BINARY mode and that you have compiled UnZip properly )  
  
  
 The zip file is not corrupt, there isn't any data loss  ( with Linux's unzip I can read all entries ) . I'm using an IBM JVM, Java 1.4.2. 
  
  
 Any suggestions?


public ZipFile(File file)
        throws ZipException,
               IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public ZipFile(File file,
               int mode)
        throws IOException
See Also:
SecurityManager.checkRead(java.lang.String), IllegalArgumentException, SecurityException, ZipException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1903]Error deleting original ZIPFILE after unzipping
By desai { dot } piyush { at } gmail { dot } com on 2007/07/12 13:47:25  Rate
Hi! 
  
  
 I am using ZipFile Class in my utility. Everything is working fine.  
 But only after unzipping the files, I want to delete the the  
 original Zipfile from its location. I am not able to delete that file. 
 Even its not allowing me to delete it from windows Explorer. 
  ( But very streng thing is if I try to overwrite the file with 
 same name using explorer it happends!!!! )  
 I have tried most of the things. 
 Anyone can help me out. 
  
  
 My Code is as under.  
  
  
 public boolean unZipFiles ( String fromZip, String toLocation )   {  
  
  
     String seperator = System.getProperty ( "file.separator" ) ; 
      
     logger.info ( "unzipping zip file "+fromZip+" to "+toLocation ) ; 
     System.out.println ( "unzipping zip file to "+toLocation ) ; 
     try  {  
            
       File zipDir = new File ( toLocation ) ; 
       zipDir.mkdir (  ) ; 
  
  
       ZipFile zip = new ZipFile ( fromZip ) ; 
       for  ( Enumeration entries = zip.entries (  ) ; entries.hasMoreElements (  ) ; )   {  
         ZipEntry e =  (  ( ZipEntry )  entries.nextElement (  )  ) ; 
         createFile ( zip.getInputStream ( e ) , new BufferedOutputStream (  
             new FileOutputStream ( toLocation + seperator + e.getName (  )  )  )  ) ; 
        }  
       zip.close (  ) ; 
      }  catch  ( Exception ex )   {  
       logger.error ( "Error unzipping zip file "+fromZip +" to "+toLocation, ex ) ; 
       return false; 
      }  
     logger.info ( "unzipped zip file "+fromZip+" to "+toLocation ) ; 
     System.out.println ( "unzipped zip file to "+toLocation ) ; 
     return true; 
    }  
 


public ZipFile(String name)
        throws IOException
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException, ZipException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags