Advanced Java Services | Lesen und Schreiben von .zip-Dateien |
Mit den beiden Klassen ZipFile und ZipEntry kann man zip-Dateien problemlos entpacken. Will man mit Java .zip Dateien erstellen, so brauch man zusätzlich die Klasse ZipOutputStream. Hier zunächst eine Übersicht über die ersten beiden Klassen.
Wichtige Felder | |
Typ | Name des Datenfelds |
static int | OPEN_DELETE Mode flag to open a zip file and mark it for deletion. |
static int | OPEN_READ Mode flag to open a zip file for reading. |
Wichtige Konstruktoren | |
ZipFile(File file) | Opens a ZIP file for reading given the specified File object. |
ZipFile(File file, int mode) | Opens a new ZipFile to read from the specified File object in the specified mode. The mode argument must be either OPEN_READ or OPEN_READ | OPEN_DELETE. |
ZipFile(String name) | Opens a zip file for reading. |
Wichtige Methoden | |
Returntyp | Name der Methode |
void | close() Closes the ZIP file. |
Enumeration | entries() Returns an enumeration of the ZIP file entries. |
protected void | finalize() Ensures that the close method of this ZIP file is called when there are no more references to it. |
ZipEntry | getEntry(String name) Returns the zip file entry for the specified name, or null if not found. |
InputStream | getInputStream(ZipEntry entry) Returns an input stream for reading the contents of the specified zip file entry. |
String | getName() Returns the path name of the ZIP file. |
int | size() Returns the number of entries in the ZIP file. |
Wichtige Konstruktoren | |
ZipEntry(String name) | Creates a new zip entry with the specified name. |
ZipEntry(ZipEntry e) | Creates a new zip entry with fields taken from the specified zip entry. |
Wichtige Methoden | |
Returntyp | Name der Methode |
Object | clone() Returns a copy of this entry. |
String | getComment() Returns the comment string for the entry, or null if none. |
void | setComment(String comment) Sets the optional comment string for the entry. |
long | getCompressedSize() Returns the size of the compressed entry data, or -1 if not known. |
void | setCompressedSize(long csize) Sets the size of the compressed entry data. |
long | getCrc() Returns the CRC-32 checksum of the uncompressed entry data, or -1 if not known. |
void | setCrc(long crc) Sets the CRC-32 checksum of the uncompressed entry data. |
byte[] | getExtra() Returns the extra field data for the entry, or null if none. |
void | setExtra(byte[] extra) Sets the optional extra field data for the entry. |
int | getMethod() Returns the compression method of the entry, or -1 if not specified. |
void | setMethod(int method) Sets the compression method for the entry. |
String | getName() Returns the name of the entry. |
long | getSize() Returns the uncompressed size of the entry data, or -1 if not known. |
void | setSize(long size) Sets the uncompressed size of the entry data. |
long | getTime() Returns the modification time of the entry, or -1 if not specified. |
void | setTime(long time) Sets the modification time of the entry. |
boolean | isDirectory() Returns true if this is a directory entry. |
try { ZipFile zipFile = new ZipFile("irgendeine.zip") ; } catch(IOException ex) { //... }
Enumeration enu = zipFile.entries() ; while (enu.hasMoreElements()) { ZipEntry zipEntry = (ZipEntry)enu.nextElement() ; System.out.println("Dateiname " +zipEntry.getName()); System.out.println("Dateigroesse " + zipEntry.getSize()); System.out.println("komprimiert " + zipEntry.getCompressedSize()); // etc. }
Die Enumeration liefert Objekte vom Typ ZipEntry, was man mit .getClass().getName() feststellen kann. zipEntry.getName() liefert den Dateinamen inklusive Pfad.
Datei wird beim Einlesen entzippt !
BufferedInputStream bis = null; try { bis = new BufferedInputStream( zipFile.getInputStream(zipEntry) ); byte[] buffer; int avail = bis.available(); if ( avail>0 ) { buffer = new byte[avail] ; bis.read(buffer, 0, avail) ; } } catch(IOException ex) { //... } finally { try { if(bis!=null) bis.close(); } catch(Exception ex) { } }
BufferedOutputStream bos = null; try { String fileName = zipEntry.getName() ; bos = new BufferedOutputStream( new FileOutputStream(fileName) ); bos.write(buffer, 0, buffer.length) ; } catch(IOException ex) { //... } finally { try { if(bos!=null) bos.close(); } catch(Exception ex) { } }
Für das Erstellen von zip-Archiven braucht man zusätzlich noch die Klasse ZipOutputStream.
Wichtige Konstruktoren | |
ZipOutputStream(OutputStream out) | Creates a new ZIP output stream. |
Wichtige Methoden | |
Returntyp | Name der Methode |
void | closeEntry() Closes the current ZIP entry and positions the stream for writing the next entry. |
void | closeEntry() Closes the current ZIP entry and positions the stream for writing the next entry. |
void | putNextEntry(ZipEntry e) Begins writing a new ZIP file entry and positions the stream to the start of the entry data. |
void | setComment(String comment) Sets the ZIP file comment. |
void | setLevel(int level) Sets the compression level for subsequent entries which are DEFLATED. |
void | setMethod(int method) Sets the default compression method for subsequent entries. |
ZipOutputStream zipOut = null; try { zipOut = new ZipOutputStream( new FileOutputStream("test.zip") ) ; } catch(IOException ex) { //... } finally { try { if(zipOut!=null) zipOut.close(); } catch(Exception ex) { } }
BufferedInputStream bis = null; try { String fileName = "pfad/zu/datei/irgendeine.xxx" ; bis = new BufferedInputStream( new FileInputStream(fileName) ); int avail = bis.available(); byte[] buffer = new byte[avail] ; if ( avail>0 ) { bis.read(buffer, 0, avail) ; } } catch(IOException ex) { //... } finally { try { if(bis!=null) bis.close(); } catch(Exception ex) { } }
ZipEntry ze = new ZipEntry(fileName); // hier wird der pfad berücksichtigt zipOut.putNextEntry(ze);
Der Eintrag muß vor der Datei kommen, sonst wird eine ZipException geworfen.
zipOut.write(buffer, 0, buffer.length); zipOut.closeEntry(); // ZipEintrag schließen
Will man mehrere Dateien in das zip-Archiv packen, so wiederholt man die letzten beiden Vorgänge.
Legt man einen neuen ZipEintrag an, so braucht man keine der set-Methoden verwenden. Beim anschließenden Schreiben der Datei in das Archiv werden die meisten der Attribute automatisch gesetzt. Liest man eine selbstgepackte .zip-Datei wieder aus, so stellt man fest, daß die Attribute Size, CompressedSize, Method, Time und Crc automatisch gesetzt werden. Nicht gesetzt werden dagegen die Attribute Comment und Extra.