1 | // Copyright FreeHEP, 2005. |
---|
2 | |
---|
3 | #include <iostream> |
---|
4 | #include <ctime> |
---|
5 | #include <vector> |
---|
6 | |
---|
7 | #include "cheprep/ZipOutputStream.h" |
---|
8 | #include "cheprep/ZipOutputStreamBuffer.h" |
---|
9 | #include "cheprep/ZipEntry.h" |
---|
10 | |
---|
11 | /** |
---|
12 | * @author Mark Donszelmann |
---|
13 | * @version $Id: ZipOutputStreamBuffer.cc,v 1.9 2005/06/02 21:28:45 duns Exp $ |
---|
14 | */ |
---|
15 | namespace cheprep { |
---|
16 | |
---|
17 | ZipOutputStreamBuffer::ZipOutputStreamBuffer(std::streambuf* buffer) |
---|
18 | : DeflateOutputStreamBuffer(buffer), |
---|
19 | comment("") { |
---|
20 | |
---|
21 | closed = false; |
---|
22 | entry = NULL; |
---|
23 | |
---|
24 | entries = new std::vector<ZipEntry*>(); |
---|
25 | } |
---|
26 | |
---|
27 | int ZipOutputStreamBuffer::overflow(int c) { |
---|
28 | return DeflateOutputStreamBuffer::overflow(c); |
---|
29 | } |
---|
30 | |
---|
31 | void ZipOutputStreamBuffer::closeEntry() { |
---|
32 | if (closed) return; |
---|
33 | if (entry == NULL) return; |
---|
34 | |
---|
35 | finish(); |
---|
36 | |
---|
37 | entry->crc = getCRC(); |
---|
38 | // NOTE: pos()::operator- is ambiguous on gcc 4.0, so convert to long first |
---|
39 | entry->csize = (long)pos() - entry->data; |
---|
40 | entry->size = getSize(); |
---|
41 | putUI(EXTSIG); |
---|
42 | putUI(entry->crc); // crc |
---|
43 | putUI(entry->csize); // compressed size |
---|
44 | putUI(entry->size); // uncompressed size |
---|
45 | entry = NULL; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | void ZipOutputStreamBuffer::close() { |
---|
50 | if (closed) return; |
---|
51 | closeEntry(); |
---|
52 | |
---|
53 | long dirStart = pos(); |
---|
54 | for (std::vector<ZipEntry*>::iterator i=entries->begin(); i != entries->end(); i++) { |
---|
55 | entry = *i; |
---|
56 | putUI(CENSIG); |
---|
57 | putUS(VERSIONMADE); // made by version |
---|
58 | putUS(VERSIONEXTRACT); // extraction version |
---|
59 | putUS(GENFLAG); // general purpose bit flag |
---|
60 | putUS(entry->method); // compression method |
---|
61 | putUS(entry->time); // time |
---|
62 | putUS(entry->date); // date |
---|
63 | putUI(entry->crc); // crc |
---|
64 | putUI(entry->csize); // compressed size |
---|
65 | putUI(entry->size); // uncompressed size |
---|
66 | putUS(entry->name.length()); // file name length |
---|
67 | putUS(0); // extra field length |
---|
68 | putUS(0); // file comment length |
---|
69 | putUS(0); // disk number start |
---|
70 | putUS(0); // internal file attributes |
---|
71 | putUI(0); // external file attributes |
---|
72 | putUI(entry->offset); // relative offset of local file header |
---|
73 | putS(entry->name); // file name |
---|
74 | |
---|
75 | // delete entry |
---|
76 | delete entry; |
---|
77 | entry = NULL; |
---|
78 | } |
---|
79 | // NOTE: pos()::operator- is ambiguous on gcc 4.0, so convert to long first |
---|
80 | long dirSize = (long)pos() - dirStart; |
---|
81 | putUI(ENDSIG); |
---|
82 | putUS(0); // number of this disk |
---|
83 | putUS(0); // number of the disk with the start of central directory |
---|
84 | putUS(entries->size()); // total number of entries in the central directory of this disk |
---|
85 | putUS(entries->size()); // total number of entries in the central directory |
---|
86 | putUI(dirSize); // size of the central directory |
---|
87 | putUI(dirStart); // offset of the start of central directory with respect to the starting disk number |
---|
88 | putUS(comment.length()); // .ZIP file comment length |
---|
89 | putS(comment); // .ZIP file comment |
---|
90 | |
---|
91 | delete entries; |
---|
92 | entries = NULL; |
---|
93 | closed = true; |
---|
94 | } |
---|
95 | |
---|
96 | void ZipOutputStreamBuffer::putNextEntry(const std::string& name, bool compress) { |
---|
97 | if (closed) return; |
---|
98 | |
---|
99 | closeEntry(); |
---|
100 | |
---|
101 | #ifdef CHEPREP_NO_ZLIB |
---|
102 | compress = false; |
---|
103 | #endif |
---|
104 | init(compress); |
---|
105 | |
---|
106 | entry = new ZipEntry(); |
---|
107 | entries->push_back(entry); |
---|
108 | |
---|
109 | entry->name = name; |
---|
110 | entry->method = compress ? 8 : 0; |
---|
111 | |
---|
112 | time_t ltime; |
---|
113 | time( <ime ); |
---|
114 | struct tm *utc = gmtime( <ime ); |
---|
115 | entry->date = (utc->tm_year - 80) << 9 | (utc->tm_mon + 1) << 5 | utc->tm_mday; |
---|
116 | entry->time = utc->tm_hour << 11 | utc->tm_min << 5 | utc->tm_sec >> 1; |
---|
117 | |
---|
118 | entry->offset = (long)pos(); |
---|
119 | putUI(LOCSIG); // signature |
---|
120 | putUS(VERSIONEXTRACT); // extraction version |
---|
121 | putUS(GENFLAG); // general purpose bit flag |
---|
122 | putUS(entry->method); // compression method |
---|
123 | putUS(entry->time); // time |
---|
124 | putUS(entry->date); // date |
---|
125 | putUI(0x00000000); // crc ATEND |
---|
126 | putUI(0x00000000); // compressed size ATEND |
---|
127 | putUI(0x00000000); // uncompressed size ATEND |
---|
128 | putUS(entry->name.length()); // file name length |
---|
129 | putUS(0); // extra field length |
---|
130 | putS(entry->name); // file name |
---|
131 | entry->data = (long)pos(); |
---|
132 | entry->crc = 0; |
---|
133 | } |
---|
134 | |
---|
135 | void ZipOutputStreamBuffer::setComment(const std::string& c) { |
---|
136 | if (closed) return; |
---|
137 | comment = c; |
---|
138 | } |
---|
139 | |
---|
140 | ZipOutputStreamBuffer::~ZipOutputStreamBuffer() { |
---|
141 | close(); |
---|
142 | } |
---|
143 | |
---|
144 | } // cheprep |
---|