17 Eylül 2019 Salı

ZipFile Sınıfı

Giriş
Şu satırı dahil ederiz.
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
constructor - File
Örnek
Şöyle yaparız.
Path pathToFile = ...
ZipFile zipFile = new ZipFile(pathToFile.toFile());
Örnek
Şöyle yaparız.
URL zipUrl = Main.class.getResource("/resources/zipfile.zip");
File zipFile = new File(zipUrl.toURI());
ZipFile zip = new ZipFile(zipFile);
entries metodu
ZipEntry dizisi döner. Girdilere erişmemizi sağlar. Şöyle yaparız.
ZipFile zipFile = ...
Enumeration<? extends ZipEntry> entries = zipFile.entries();

while (entries.hasMoreElements()) {
  ZipEntry entry = entries.nextElement();
  ...
}
getFileHeaders metodu
FileHeader dizisi döner. Açıklaması şöyle.
The end of a Zip file is indicated by the end of central directory record (EOCD). The length of the EOCD is variable because it can contain a comment up to 65535 bytes long. See the bold part of the EOCD layout below:
EOCD şöyledir
+---------+--------+--------------------------------------------------------------------+
| Offset  | Bytes  |                            Description                             |
+---------+--------+--------------------------------------------------------------------+
|      0  | 4      | End of central directory signature = 0x06054b50                    |
|      4  | 2      | Number of this disk                                                |
|      6  | 2      | Disk where central directory starts                                |
|      8  | 2      | Number of central directory records on this disk                   |
|     10  | 2      | Total number of central directory records                          |
|     12  | 4      | Size of central directory (bytes)                                  |
|     16  | 4      | Offset of start of central directory, relative to start of archive |
|     20  | 2      | Comment length (n)                                                 |
|     22  | n      | Comment                                                            |
+---------+--------+--------------------------------------------------------------------+
size metodu
Kaç tane girdi (dosya veye dizin) olduğunu döndürür. Şöyle yaparız.
if (zipFile.size() != 1) {...}


Hiç yorum yok:

Yorum Gönder