1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
// -*-c++-*-
// fixg2sxd - a utility to convert fig to sxd format
// Copyright (C) 2003-2022 Alexander Bürger, acfb@users.sourceforge.net
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "zipwrite.h"
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <streambuf>
using namespace std;
// ------------------------------------------------------------------------
class WriterBuf : public std::streambuf
{
public:
typedef int (*WriteFunc)( void* user, const char* buf, unsigned length );
private:
WriteFunc writefunc;
void* user;
unsigned bufsize;
char *buf;
public:
WriterBuf( WriteFunc wf, void* u, unsigned bs = 1)
: writefunc( wf ),
user( u ),
bufsize( bs < 1 ? 1 : bs ),
buf( new char[ bufsize ] )
{
setp( buf, buf+bufsize );
}
~WriterBuf();
protected:
int overflow( int c );
int sync();
};
WriterBuf::~WriterBuf()
{
if( buf ) {
sync();
delete[] buf;
}
}
int WriterBuf::overflow( int c )
{
sync();
if( c != EOF ) {
*pptr() = static_cast<char>( c );
pbump( 1 );
}
return c;
}
int WriterBuf::sync()
{
int ret = 0;
if( pptr() > pbase() )
{
ret = writefunc( user, buf, pptr() - pbase() );
setp( buf, buf+bufsize );
}
return ret;
}
// ------------------------------------------------------------------------
ostream& write2( ostream& out, unsigned short s )
{
unsigned char bytes[2] = {
static_cast<unsigned char>(s&0xff),
static_cast<unsigned char>((s>>8)&0xff)
};
out.write( (const char*)bytes, 2 );
return out;
}
ostream& write4( ostream& out, unsigned int i )
{
unsigned char bytes[4] = {
static_cast<unsigned char>(i&0xff),
static_cast<unsigned char>((i>>8)&0xff),
static_cast<unsigned char>((i>>16)&0xff),
static_cast<unsigned char>((i>>24)&0xff)
};
out.write( (const char*)bytes, 4 );
return out;
}
// ------------------------------------------------------------------------
ZipWriter::~ZipWriter()
{
Close();
const int rel_offset_central_dir = zip.tellp();
// write central directory
for( fileinfos_t::iterator i=fileinfos.begin(); i!=fileinfos.end(); ++i )
{
fileinfo& fi = *i;
// central file header signature
write4( zip, 0x02014b50 );
write2( zip, 20 );
write2( zip, 20 );
// general_purpose_bits, crc and size come later
write2( zip, (1<<3 | 1<<1));
// compression method
write2( zip, 8 );
write2( zip, fi.last_mod_file_time );
write2( zip, fi.last_mod_file_date );
write4( zip, fi.crc32 );
write4( zip, fi.compressed_size );
write4( zip, fi.uncompressed_size );
write2( zip, fi.file_name.length() );
// extra field length, file comment length, disk number start,
// internal file attributes (each 2 bytes)
write4( zip, 0 );
write4( zip, 0 );
// external file attributes
write4( zip, 0 );
write4( zip, fi.rel_offset_local_header );
zip.write( fi.file_name.data(), fi.file_name.length() );
}
const int rel_end_central_dir = zip.tellp();
const int size_central_dir = rel_end_central_dir - rel_offset_central_dir;
// write end of central dir
write4( zip, 0x06054b50 );
// disk number
write2( zip, 0 );
// number of the disk with the start of the central directory
write2( zip, 0 );
// n.o. entries in the centr. dir. on this disk
write2( zip, fileinfos.size() );
// total number of entries in the central dir.
write2( zip, fileinfos.size() );
// size of the central directory (4 bytes)
write4( zip, size_central_dir );
// offset of start of central directory with respect to the
// starting disk number
write4( zip, rel_offset_central_dir );
// .ZIP file comment length -- comment is empty (2 bytes)
write2( zip, 0 );
}
ostream& ZipWriter::GetStream( const char* name )
{
if( out )
Close();
fileinfo fi;
fi.rel_offset_local_header = zip.tellp();
time_t now = time(0);
struct tm* t = localtime(&now);
fi.last_mod_file_time =
(t->tm_sec/2)+(32*t->tm_min)+(2048*(uLong)t->tm_hour);
fi.last_mod_file_date =
(t->tm_mday)+(32*(t->tm_mon+1))+(512*(t->tm_year-1900));
// write local file header:
// local_file_header_signature
write4( zip, 0x04034b50 );
write2( zip, 20 );
write2( zip, (1<<3 | 1<<1));
// compression method
write2( zip, 8 );
write2( zip, fi.last_mod_file_time );
write2( zip, fi.last_mod_file_date );
write4( zip, fi.crc32 = 0 );
write4( zip, fi.compressed_size = 0 );
write4( zip, fi.uncompressed_size = 0 );
fi.file_name = name;
write2( zip, fi.file_name.length() );
write2( zip, 0 );
zip.write( fi.file_name.data(), fi.file_name.length() );
zs.zalloc = 0; zs.zfree = 0; zs.opaque = 0;
zs.total_in = zs.total_out = 0;
zs.next_out = (Bytef*)cbuf;
zs.avail_out = sizeof(cbuf);
if( deflateInit2( &zs, 9, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL,
Z_DEFAULT_STRATEGY ) != Z_OK )
{
cerr << "deflateInit2" << endl;
exit( -1 );
}
fi.crc32 = crc32( 0, Z_NULL, 0 );
fileinfos.push_back( fi );
out = new ostream( new WriterBuf( writefunc, this, 32768 ) );
return *out;
}
void ZipWriter::Close()
{
if( !out )
return;
// write everything
streambuf* sb = out->rdbuf();
delete out;
delete sb;
out = 0;
// finish zip stream
int err = Z_OK;
while( err == Z_OK
&& ( err = deflate( &zs, Z_FINISH )) == Z_OK )
{
zip.write( cbuf, sizeof(cbuf) );
zs.next_out = (Bytef*)cbuf;
zs.avail_out = sizeof(cbuf);
}
if( err == Z_STREAM_END ) {
zip.write( cbuf, sizeof(cbuf)-zs.avail_out );
} else {
cerr << "zip error" << endl;
exit( -1 );
}
if( deflateEnd( &zs ) != Z_OK ) {
cerr << "deflateEnd" << endl;
exit( -1 );
}
fileinfo& fi = fileinfos.back();
fi.uncompressed_size = zs.total_in;
fi.compressed_size = zs.total_out;
// write crc and sizes
write4( zip, fi.crc32 );
write4( zip, fi.uncompressed_size );
write4( zip, fi.compressed_size );
}
int ZipWriter::Write( const char* buf, unsigned length )
{
zs.next_in = (Bytef*)buf;
zs.avail_in = length;
fileinfos.back().crc32 = crc32(fileinfos.back().crc32,(Bytef*)buf,length);
int err = Z_OK;
while( err == Z_OK && zs.avail_in>0 ) {
if( zs.avail_out == 0 ) {
//flush, reset cbuf
zip.write( cbuf, sizeof(cbuf) );
zs.next_out = (Bytef*)cbuf;
zs.avail_out = sizeof(cbuf);
}
err = deflate( &zs, Z_NO_FLUSH );
}
if( err != Z_OK ) {
cerr << "while 1" << endl;
exit( -1 );
}
return 0;
}
int ZipWriter::writefunc( void* user, const char* buf, unsigned length )
{
return ((ZipWriter*)user)->Write( buf, length );
}
// ------------------------------------------------------------------------
#ifdef TEST_ZIPWRITE
int main( int argc, char* argv[] )
{
if( argc < 3 ) {
cerr << "need 3 args" << endl;
exit( -1 );
}
std::ofstream zipout( argv[1] );
ZipWriter zw( zipout );
for( int f = 2; f<argc; ++f ) {
ifstream file( argv[f], ios::binary );
if( !file ) {
cerr << "cannot read '" << argv[f] << "' - skipping" << endl;
continue;
}
zw.GetStream( argv[f] ) << file.rdbuf();
}
}
#endif // TEST_ZIPWRITE
|