1 package jp.sfjp.armadillo.archive.zip;
4 import jp.sfjp.armadillo.archive.*;
5 import jp.sfjp.armadillo.io.*;
7 public final class ZipArchiveCreator implements ArchiveCreator {
9 private ZipOutputStream os;
11 public ZipArchiveCreator(OutputStream os) {
12 this.os = new ZipOutputStream(os);
16 public ArchiveEntry newEntry(String name) {
17 return new ZipEntry(name);
21 public void addEntry(ArchiveEntry entry, File file) throws IOException {
22 if (file.isDirectory()) {
23 os.putNextEntry(toZipEntry(entry));
28 addEntry(entry, file, null, file.length());
32 public void addEntry(ArchiveEntry entry, InputStream is, long length) throws IOException {
33 addEntry(entry, null, is, length);
36 private void addEntry(ArchiveEntry entry, File file, InputStream src, final long length) throws IOException {
37 ZipEntry fileEntry = toZipEntry(entry);
38 fileEntry.setSize(length);
40 fileEntry.setCompressedSize(0L);
41 fileEntry.method = ZipEntry.STORED;
43 os.putNextEntry(fileEntry);
46 InputStream is = (src == null) ? new FileInputStream(file) : src;
48 size = IOUtilities.transfer(is, os, length);
54 assert size == fileEntry.getSize() : "file size";
55 assert size == length : "file size";
62 static ZipEntry toZipEntry(ArchiveEntry entry) {
63 if (entry instanceof ZipEntry)
64 return (ZipEntry)entry;
65 ZipEntry newEntry = new ZipEntry(entry.getName());
66 entry.copyTo(newEntry);
67 if (newEntry.isDirectory())
68 newEntry.method = ZipEntry.STORED;
73 public void close() throws IOException {