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