OSDN Git Service

Add new source files
[armadillo/armadillo1.git] / src / jp / sfjp / armadillo / archive / cab / CabArchiveCreator.java
1 package jp.sfjp.armadillo.archive.cab;
2
3 import java.io.*;
4 import jp.sfjp.armadillo.archive.*;
5 import jp.sfjp.armadillo.io.*;
6
7 public final class CabArchiveCreator implements ArchiveCreator {
8
9     private CabOutputStream os;
10
11     public CabArchiveCreator(OutputStream os) {
12         this.os = new CabOutputStream(os);
13     }
14
15     @Override
16     public ArchiveEntry newEntry(String name) {
17         return CabEntry.getInstance(name);
18     }
19
20     @Override
21     public void addEntry(ArchiveEntry entry, File file) throws IOException {
22         if (file.isDirectory()) {
23             os.putNextEntry(toCabEntry(entry));
24             os.closeEntry();
25         }
26         else {
27             InputStream is = new FileInputStream(file);
28             try {
29                 addEntry(entry, is, file.length());
30             }
31             finally {
32                 is.close();
33             }
34         }
35         entry.setAdded(true);
36     }
37
38     @Override
39     public void addEntry(ArchiveEntry entry, InputStream is, long length) throws IOException {
40         CabEntry fileEntry = toCabEntry(entry);
41         fileEntry.setSize(length);
42         os.putNextEntry(fileEntry);
43         final long size = IOUtilities.transferAll(is, os);
44         os.closeEntry();
45         assert size == fileEntry.getSize() : "file size";
46         assert size == length : "file size";
47         entry.setSize(size);
48         entry.setAdded(true);
49     }
50
51     static CabEntry toCabEntry(ArchiveEntry entry) {
52         if (entry instanceof CabEntry)
53             return (CabEntry)entry;
54         CabEntry newEntry = CabEntry.getInstance(entry.getName());
55         entry.copyTo(newEntry);
56         return newEntry;
57     }
58
59     @Override
60     public void close() throws IOException {
61         os.close();
62     }
63
64 }