This repository has been archived by the owner on Feb 20, 2024. It is now read-only.
forked from soniammarshall/ZipArchive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZipArchive.cc
872 lines (785 loc) · 29.5 KB
/
ZipArchive.cc
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#include "XrdCl/XrdClFile.hh"
#include "XrdCl/XrdClFileSystem.hh"
#include "XrdCl/XrdClURL.hh"
#include "XrdCl/XrdClMessageUtils.hh"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <string>
#include <stdint.h>
#include <ctime>
#include <cstring>
#include <errno.h>
#include <vector>
#include <memory>
#include <exception>
namespace XrdCl
{
const uint16_t ovrflw16 = 0xffff;
const uint32_t ovrflw32 = 0xffffffff;
const uint64_t ovrflw64 = 0xffffffffffffffff;
// taken from XrdClZipArchiveReader.cc
template<typename RESP>
struct ZipHandlerException
{
ZipHandlerException( XRootDStatus *status, RESP *response ) : status( status ), response( response ) { }
XRootDStatus *status;
RESP *response;
};
// ZIP64 extended information extra field
struct ZipExtra
{
ZipExtra( uint64_t fileSize )
{
offset = 0;
nbDisk = 0;
if ( fileSize >= ovrflw32 )
{
dataSize = 16;
uncompressedSize = fileSize;
compressedSize = fileSize;
totalSize = dataSize + 4;
}
else
{
dataSize = 0;
uncompressedSize = 0;
compressedSize = 0;
totalSize = 0;
}
}
ZipExtra( ZipExtra *extra, uint64_t offset )
{
nbDisk = 0;
uncompressedSize = extra->uncompressedSize;
compressedSize = extra->compressedSize;
dataSize = extra->dataSize;
totalSize = extra->totalSize;
if ( offset >= ovrflw32 )
{
this->offset = offset;
dataSize += 8;
totalSize = dataSize + 4;
}
else
this->offset = 0;
}
void Write( File &archive, uint64_t writeOffset )
{
if ( totalSize > 0 )
{
std::unique_ptr<char[]> buffer { new char[totalSize] };
std::memcpy( buffer.get(), &headerID, 2 );
std::memcpy( buffer.get() + 2, &dataSize, 2 );
if ( uncompressedSize > 0)
{
std::memcpy( buffer.get() + 4, &uncompressedSize, 8 );
std::memcpy( buffer.get() + 12, &compressedSize, 8 );
if ( offset > 0 )
std::memcpy( buffer.get() + 20, &offset, 8 );
}
else if ( offset > 0 )
std::memcpy( buffer.get() + 4, &offset, 8 );
XRootDStatus st = archive.Write( writeOffset, totalSize, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
}
static const uint16_t headerID = 0x0001;
uint16_t dataSize;
uint64_t uncompressedSize;
uint64_t compressedSize;
uint64_t offset;
uint32_t nbDisk;
uint16_t totalSize;
};
// local file header
struct LFH
{
LFH( std::string filename, uint32_t crc, off_t fileSize, time_t time )
{
generalBitFlag = 0;
compressionMethod = 0;
ZCRC32 = crc;
if ( fileSize >= ovrflw32 )
{
compressedSize = ovrflw32;
uncompressedSize = ovrflw32;
}
else
{
compressedSize = fileSize;
uncompressedSize = fileSize;
}
extra = new ZipExtra( fileSize );
extraLength = extra->totalSize;
if ( extraLength == 0 )
minZipVersion = 10;
else
minZipVersion = 45;
this->filename = filename;
filenameLength = this->filename.length();
ToMsdosDateTime( &time );
lfhSize = lfhBaseSize + filenameLength + extraLength;
}
void ToMsdosDateTime( time_t *originalTime )
{
// convert from Epoch time to local time
struct tm *t = localtime( originalTime );
// convert to MS-DOS time format
uint16_t hour = t->tm_hour;
uint16_t min = t->tm_min;
uint16_t sec = t->tm_sec / 2;
uint16_t year = t->tm_year - 80;
uint16_t month = t->tm_mon + 1;
uint16_t day = t->tm_mday;
lastModFileTime = ( hour << 11 ) | ( min << 5 ) | sec ;
lastModFileDate = ( year << 9 ) | ( month << 5 ) | day ;
}
void Write( File &archive, uint64_t writeOffset )
{
uint16_t size = lfhSize - extraLength;
std::unique_ptr<char[]> buffer { new char[size] };
std::memcpy( buffer.get(), &lfhSign, 4 );
std::memcpy( buffer.get() + 4, &minZipVersion, 2 );
std::memcpy( buffer.get() + 6, &generalBitFlag, 2 );
std::memcpy( buffer.get() + 8, &compressionMethod, 2 );
std::memcpy( buffer.get() + 10, &lastModFileTime, 2 );
std::memcpy( buffer.get() + 12, &lastModFileDate, 2 );
std::memcpy( buffer.get() + 14, &ZCRC32, 4 );
std::memcpy( buffer.get() + 18, &compressedSize, 4 );
std::memcpy( buffer.get() + 22, &uncompressedSize, 4 );
std::memcpy( buffer.get() + 26, &filenameLength, 2 );
std::memcpy( buffer.get() + 28, &extraLength, 2 );
std::memcpy( buffer.get() + 30, filename.c_str(), filenameLength );
XRootDStatus st = archive.Write( writeOffset, size, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
writeOffset += size;
if ( extraLength > 0 )
extra->Write( archive, writeOffset );
}
uint16_t minZipVersion;
uint16_t generalBitFlag;
uint16_t compressionMethod;
uint16_t lastModFileTime;
uint16_t lastModFileDate;
uint32_t ZCRC32;
uint32_t compressedSize;
uint32_t uncompressedSize;
uint16_t filenameLength;
uint16_t extraLength;
std::string filename;
ZipExtra *extra;
uint16_t lfhSize;
static const uint16_t lfhBaseSize = 30;
static const uint32_t lfhSign = 0x04034b50;
};
// central directory file header
struct CDFH
{
CDFH( LFH *lfh, mode_t mode, uint64_t lfhOffset )
{
zipVersion = ( 3 << 8 ) | 63;
generalBitFlag = lfh->generalBitFlag;
compressionMethod = lfh->compressionMethod;
lastModFileTime = lfh->lastModFileTime;
lastModFileDate = lfh->lastModFileDate;
ZCRC32 = lfh->ZCRC32;
compressedSize = lfh->compressedSize;
uncompressedSize = lfh->uncompressedSize;
filenameLength = lfh->filenameLength;
commentLength = 0;
nbDisk = 0;
internAttr = 0;
externAttr = mode << 16;
if ( lfhOffset >= ovrflw32 )
offset = ovrflw32;
else
offset = lfhOffset;
extra = new ZipExtra( lfh->extra, lfhOffset );
extraLength = extra->totalSize;
if ( extraLength == 0 )
minZipVersion = 10;
else
minZipVersion = 45;
filename = lfh->filename;
comment = "";
cdfhSize = cdfhBaseSize + filenameLength + extraLength + commentLength;
}
void Write( File &archive, uint64_t writeOffset )
{
uint16_t size = cdfhSize - extraLength - commentLength;
std::unique_ptr<char[]> buffer { new char[size] };
std::memcpy( buffer.get(), &cdfhSign, 4 );
std::memcpy( buffer.get() + 4, &zipVersion, 2 );
std::memcpy( buffer.get() + 6, &minZipVersion, 2 );
std::memcpy( buffer.get() + 8, &generalBitFlag, 2 );
std::memcpy( buffer.get() + 10, &compressionMethod, 2 );
std::memcpy( buffer.get() + 12, &lastModFileTime, 2 );
std::memcpy( buffer.get() + 14, &lastModFileDate, 2 );
std::memcpy( buffer.get() + 16, &ZCRC32, 4 );
std::memcpy( buffer.get() + 20, &compressedSize, 4 );
std::memcpy( buffer.get() + 24, &uncompressedSize, 4 );
std::memcpy( buffer.get() + 28, &filenameLength, 2 );
std::memcpy( buffer.get() + 30, &extraLength, 2 );
std::memcpy( buffer.get() + 32, &commentLength, 2 );
std::memcpy( buffer.get() + 34, &nbDisk, 2 );
std::memcpy( buffer.get() + 36, &internAttr, 2 );
std::memcpy( buffer.get() + 38, &externAttr, 4 );
std::memcpy( buffer.get() + 42, &offset, 4 );
std::memcpy( buffer.get() + 46, filename.c_str(), filenameLength );
XRootDStatus st = archive.Write( writeOffset, size, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
writeOffset += size;
if ( extraLength > 0 )
{
extra->Write( archive, writeOffset );
writeOffset += extraLength;
}
if ( commentLength > 0 )
{
st = archive.Write( writeOffset, commentLength, comment.c_str() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
}
uint16_t zipVersion;
uint16_t minZipVersion;
uint16_t generalBitFlag;
uint16_t compressionMethod;
uint16_t lastModFileTime;
uint16_t lastModFileDate;
uint32_t ZCRC32;
uint32_t compressedSize;
uint32_t uncompressedSize;
uint16_t filenameLength;
uint16_t extraLength;
uint16_t commentLength;
uint16_t nbDisk;
uint16_t internAttr;
uint32_t externAttr;
uint32_t offset;
std::string filename;
ZipExtra *extra;
std::string comment;
uint16_t cdfhSize;
static const uint16_t cdfhBaseSize = 46;
static const uint32_t cdfhSign = 0x02014b50;
};
// end of central directory record
struct EOCD
{
// constructor used when reading from existing ZIP archive
EOCD( const char *buffer )
{
nbDisk = *reinterpret_cast<const uint16_t*>( buffer + 4 );
nbDiskCd = *reinterpret_cast<const uint16_t*>( buffer + 6 );
nbCdRecD = *reinterpret_cast<const uint16_t*>( buffer + 8 );
nbCdRec = *reinterpret_cast<const uint16_t*>( buffer + 10 );
cdSize = *reinterpret_cast<const uint32_t*>( buffer + 12 );
cdOffset = *reinterpret_cast<const uint32_t*>( buffer + 16 );
commentLength = *reinterpret_cast<const uint16_t*>( buffer + 20 );
comment = std::string( buffer + 22, commentLength );
eocdSize = eocdBaseSize + commentLength;
useZip64= false;
}
// constructor used when creating new ZIP archive
EOCD(LFH *lfh, CDFH *cdfh )
{
useZip64 = false;
nbDisk = 0;
nbDiskCd = 0;
nbCdRecD = 1;
nbCdRec = 1;
if ( lfh->compressedSize == ovrflw32 || lfh->lfhSize + lfh->compressedSize >= ovrflw32 )
{
cdOffset = ovrflw32;
cdSize = ovrflw32;
useZip64 = true;
}
else
{
cdOffset = lfh->lfhSize + lfh->compressedSize;
cdSize = cdfh->cdfhSize;
}
commentLength = 0;
comment = "";
eocdSize = eocdBaseSize + commentLength;
}
void Write( File &archive, uint64_t writeOffset )
{
std::unique_ptr<char[]> buffer { new char[eocdSize] };
std::memcpy( buffer.get(), &eocdSign, 4 );
std::memcpy( buffer.get() + 4, &nbDisk, 2 );
std::memcpy( buffer.get() + 6, &nbDiskCd, 2 );
std::memcpy( buffer.get() + 8, &nbCdRecD, 2 );
std::memcpy( buffer.get() + 10, &nbCdRec, 2 );
std::memcpy( buffer.get() + 12, &cdSize, 4 );
std::memcpy( buffer.get() + 16, &cdOffset, 4 );
std::memcpy( buffer.get() + 20, &commentLength, 2 );
if ( commentLength > 0 )
std::memcpy( buffer.get() + 22, comment.c_str(), commentLength );
XRootDStatus st = archive.Write( writeOffset, eocdSize, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
uint16_t nbDisk;
uint16_t nbDiskCd;
uint16_t nbCdRecD;
uint16_t nbCdRec;
uint32_t cdSize;
uint32_t cdOffset;
uint16_t commentLength;
std::string comment;
uint16_t eocdSize;
bool useZip64;
static const uint16_t eocdBaseSize = 22;
static const uint32_t eocdSign = 0x06054b50;
static const uint16_t maxCommentLength = 65535;
};
// ZIP64 end of central directory record
struct ZIP64_EOCD
{
// constructor used when reading from existing ZIP archive
ZIP64_EOCD( const char* buffer )
{
zip64EocdSize = *reinterpret_cast<const uint64_t*>( buffer + 4 );
zipVersion = *reinterpret_cast<const uint16_t*>( buffer + 12 );
minZipVersion = *reinterpret_cast<const uint16_t*>( buffer + 14 );
nbDisk = *reinterpret_cast<const uint32_t*>( buffer + 16 );
nbDiskCd = *reinterpret_cast<const uint32_t*>( buffer + 20 );
nbCdRecD = *reinterpret_cast<const uint64_t*>( buffer + 24 );
nbCdRec = *reinterpret_cast<const uint64_t*>( buffer + 32 );
cdSize = *reinterpret_cast<const uint64_t*>( buffer + 40 );
cdOffset = *reinterpret_cast<const uint64_t*>( buffer + 48 );
extensibleData = "";
extensibleDataLength = 0;
zip64EocdTotalSize = zip64EocdBaseSize + extensibleDataLength;
}
ZIP64_EOCD( EOCD *eocd,
LFH *lfh,
CDFH *cdfh,
uint16_t prevNbCdRecD = 0,
uint16_t prevNbCdRec = 0,
uint32_t prevCdSize = 0,
uint32_t prevCdOffset = 0 )
{
zipVersion = ( 3 << 8 ) | 63;
minZipVersion = 45;
nbDisk = eocd->nbDisk;
nbDiskCd = eocd->nbDiskCd;
if ( eocd->nbCdRecD == ovrflw16 )
nbCdRecD = prevNbCdRecD + 1;
else
nbCdRecD = eocd->nbCdRecD;
if ( eocd->nbCdRec == ovrflw16 )
nbCdRec = prevNbCdRec + 1;
else
nbCdRec = eocd->nbCdRec;
if ( eocd->cdSize == ovrflw32 )
cdSize = prevCdSize + cdfh->cdfhSize;
else
cdSize = eocd->cdSize;
if ( eocd->cdOffset == ovrflw32 )
{
if ( lfh->compressedSize == ovrflw32 )
cdOffset = prevCdOffset + lfh->lfhSize + lfh->extra->compressedSize;
else
cdOffset = prevCdOffset + lfh->lfhSize + lfh->compressedSize;
}
else
cdOffset = eocd->cdOffset;
extensibleData = "";
extensibleDataLength = 0;
zip64EocdSize = zip64EocdBaseSize + extensibleDataLength - 12;
zip64EocdTotalSize = zip64EocdBaseSize + extensibleDataLength;
}
void Write( File &archive, uint64_t writeOffset )
{
std::unique_ptr<char[]> buffer { new char[zip64EocdTotalSize] };
std::memcpy( buffer.get(), &zip64EocdSign, 4 );
std::memcpy( buffer.get() + 4, &zip64EocdSize, 8 );
std::memcpy( buffer.get() + 12, &zipVersion, 2 );
std::memcpy( buffer.get() + 14, &minZipVersion, 2 );
std::memcpy( buffer.get() + 16, &nbDisk, 4 );
std::memcpy( buffer.get() + 20, &nbDiskCd, 4 );
std::memcpy( buffer.get() + 24, &nbCdRecD, 8 );
std::memcpy( buffer.get() + 32, &nbCdRec, 8 );
std::memcpy( buffer.get() + 40, &cdSize, 8 );
std::memcpy( buffer.get() + 48, &cdOffset, 8 );
if ( extensibleDataLength > 0 )
std::memcpy( buffer.get() + 56, extensibleData.c_str(), extensibleDataLength );
XRootDStatus st = archive.Write( writeOffset, zip64EocdTotalSize, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
uint64_t zip64EocdSize;
uint16_t zipVersion;
uint16_t minZipVersion;
uint32_t nbDisk;
uint32_t nbDiskCd;
uint64_t nbCdRecD;
uint64_t nbCdRec;
uint64_t cdSize;
uint64_t cdOffset;
std::string extensibleData;
uint64_t extensibleDataLength;
uint64_t zip64EocdTotalSize;
static const uint16_t zip64EocdBaseSize = 56;
static const uint32_t zip64EocdSign = 0x06064b50;
};
// ZIP64 end of central directory locator
struct ZIP64_EOCDL
{
// constructor used when reading from existing ZIP archive
ZIP64_EOCDL( const char *buffer )
{
nbDiskZip64Eocd = *reinterpret_cast<const uint32_t*>( buffer + 4 );
zip64EocdOffset = *reinterpret_cast<const uint64_t*>( buffer + 8 );
totalNbDisks = *reinterpret_cast<const uint32_t*>( buffer + 16 );
}
ZIP64_EOCDL( EOCD *eocd, ZIP64_EOCD *zip64Eocd )
{
nbDiskZip64Eocd = 0;
totalNbDisks = 1;
if ( eocd->cdOffset == ovrflw32 )
zip64EocdOffset = zip64Eocd->cdOffset;
else
zip64EocdOffset = eocd->cdOffset;
if ( eocd->cdSize == ovrflw32 )
zip64EocdOffset += zip64Eocd->cdSize;
else
zip64EocdOffset += eocd->cdSize;
}
void Write( File &archive, uint64_t writeOffset )
{
std::unique_ptr<char[]> buffer { new char[zip64EocdlSize] };
std::memcpy( buffer.get(), &zip64EocdlSign, 4 );
std::memcpy( buffer.get() + 4, &nbDiskZip64Eocd, 4 );
std::memcpy( buffer.get() + 8, &zip64EocdOffset, 8 );
std::memcpy( buffer.get() + 16, &totalNbDisks, 4 );
XRootDStatus st = archive.Write( writeOffset, zip64EocdlSize, buffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
uint32_t nbDiskZip64Eocd;
uint64_t zip64EocdOffset;
uint32_t totalNbDisks;
static const uint16_t zip64EocdlSize = 20;
static const uint32_t zip64EocdlSign = 0x07064b50;
};
class ZipArchive
{
public:
ZipArchive( File &archive, std::string archiveUrl ) : archive( archive ),
archiveUrl( archiveUrl ),
archiveSize( 0 ),
existingCdSize( 0 ),
writeOffset( 0 ),
isOpen( false ),
createNew( false )
{
}
// open archive file for reading and writing and with file permissions 644
void Open()
{
// stat to check if file exists already
URL url( archiveUrl );
FileSystem fs( url ) ;
StatInfo *response = 0;
XRootDStatus st = fs.Stat( url.GetPath(), response );
if( st.IsOK() && response )
{
// open existing ZIP archive to append to
st = archive.Open( archiveUrl, OpenFlags::Update, Access::UR | Access::UW | Access::GR | Access::OR );
if ( st.IsOK() )
{
isOpen = true;
archiveSize = response->GetSize();
delete response;
// read EOCD into buffer
uint32_t size = EOCD::maxCommentLength + EOCD::eocdBaseSize + ZIP64_EOCDL::zip64EocdlSize;
if ( size > archiveSize ) size = archiveSize;
uint64_t offset = archiveSize - size;
buffer.reset( new char[size] );
uint32_t bytesRead = 0;
st = archive.Read( offset, size, buffer.get(), bytesRead );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
// find and store existing EOCD, ZIP64EOCD, ZIP64EOCDL and central directory records
XRootDStatus st = ReadCentralDirectory( size );
if ( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
else
throw ZipHandlerException<AnyObject>( &st, 0 );
}
else
{
// open new ZIP archive
st = archive.Open( archiveUrl, OpenFlags::New | OpenFlags::Update, Access::UR | Access::UW | Access::GR | Access::OR );
if ( st.IsOK() )
{
createNew = true;
isOpen = true;
}
else
throw ZipHandlerException<AnyObject>( &st, 0 );
}
}
// prepare archive for appending file
// create headers, update end of central directory record and write LFH to the archive
void Append( std::string filename, uint32_t crc, off_t fileSize, time_t fileModTime, mode_t fileMode )
{
LFH *lfh = new LFH( filename, crc, fileSize, fileModTime );
CDFH *cdfh;
if ( !createNew )
{
// must be appending to existing archive
if ( eocd->useZip64 )
{
cdfh = new CDFH( lfh, fileMode, zip64Eocd->cdOffset );
// update EOCD
if ( eocd->nbCdRecD + 1 >= ovrflw16 )
eocd->nbCdRecD = ovrflw16;
else
eocd->nbCdRecD += 1;
if ( eocd->nbCdRec + 1 >= ovrflw16 )
eocd->nbCdRec = ovrflw16;
else
eocd->nbCdRec += 1;
// update ZIP64 EOCD
zip64Eocd->nbCdRecD += 1;
zip64Eocd->nbCdRec += 1;
zip64Eocd->cdSize += cdfh->cdfhSize;
if ( lfh->compressedSize == ovrflw32 )
zip64Eocd->cdOffset += lfh->lfhSize + lfh->extra->compressedSize;
else
zip64Eocd->cdOffset += lfh->lfhSize + lfh->compressedSize;
// update ZIP64 EOCDL
zip64Eocdl->zip64EocdOffset = zip64Eocd->cdOffset + zip64Eocd->cdSize;
}
else
{
cdfh = new CDFH( lfh, fileMode, eocd->cdOffset );
// udpate EOCD
if ( eocd->cdSize + cdfh->cdfhSize >= ovrflw32
|| lfh->compressedSize == ovrflw32
|| eocd->cdOffset + lfh->lfhSize + lfh->compressedSize >= ovrflw32
|| eocd->nbCdRecD + 1 >= ovrflw16
|| eocd->nbCdRec + 1 >= ovrflw16 )
{
// overflown
eocd->useZip64 = true;
uint16_t prevNbCdRecD = eocd->nbCdRecD;
uint16_t prevNbCdRec = eocd->nbCdRec;
uint32_t prevCdSize = eocd->cdSize;
uint32_t prevCdOffset = eocd->cdOffset;
if ( eocd->nbCdRecD + 1 >= ovrflw16 )
eocd->nbCdRecD = ovrflw16;
else
eocd->nbCdRecD += 1;
if ( eocd->nbCdRec + 1)
eocd->nbCdRec = ovrflw16;
else
eocd->nbCdRec += 1;
eocd->cdSize = ovrflw32;
eocd->cdOffset = ovrflw32;
zip64Eocd = new ZIP64_EOCD( eocd, lfh, cdfh, prevNbCdRecD, prevNbCdRec, prevCdSize, prevCdOffset );
zip64Eocdl = new ZIP64_EOCDL( eocd, zip64Eocd );
}
else
{
eocd->nbCdRecD += 1;
eocd->nbCdRec += 1;
eocd->cdSize += cdfh->cdfhSize;
eocd->cdOffset += lfh->lfhSize + lfh->compressedSize;
}
}
}
else
{
// must be creating new archive
cdfh = new CDFH( lfh, fileMode, 0 );
eocd = new EOCD( lfh, cdfh );
if ( eocd->useZip64 )
{
zip64Eocd = new ZIP64_EOCD( eocd, lfh, cdfh );
zip64Eocdl = new ZIP64_EOCDL( eocd, zip64Eocd );
}
createNew = false;
}
cdRecords.push_back( cdfh );
// write local file header to the archive
writeOffset = ( cdfh->offset == ovrflw32 ) ? cdfh->extra->offset : cdfh->offset;
lfh->Write( archive, writeOffset );
writeOffset += lfh->lfhSize;
}
// taken from XrdClZipArchiveReader.cc (modified variable names)
char* LookForEocd( uint64_t size )
{
for( ssize_t offset = size - EOCD::eocdBaseSize; offset >= 0; --offset )
{
uint32_t *signature = reinterpret_cast<uint32_t*>( buffer.get() + offset );
if( *signature == EOCD::eocdSign ) return buffer.get() + offset;
}
return 0;
}
// taken from XrdClZipArchiveReader.cc (modified ReadCdfh())
XRootDStatus ReadCentralDirectory( uint64_t bytesRead )
{
char *eocdBlock = LookForEocd( bytesRead );
if( !eocdBlock ) throw ZipHandlerException<AnyObject>( new XRootDStatus( stError, errDataError, errDataError, "End-of-central-directory signature not found." ), 0 );
eocd = new EOCD( eocdBlock ) ;
// Let's see if it is ZIP64 (if yes, the EOCD will be preceded with ZIP64 EOCD locator)
char *zip64EocdlBlock = eocdBlock - ZIP64_EOCDL::zip64EocdlSize;
// make sure there is enough data to assume there's a ZIP64 EOCD locator
if( zip64EocdlBlock > buffer.get() )
{
uint32_t *signature = reinterpret_cast<uint32_t*>( zip64EocdlBlock );
if( *signature == ZIP64_EOCDL::zip64EocdlSign )
{
zip64Eocdl = new ZIP64_EOCDL( zip64EocdlBlock );
// the offset at which we did the read
uint64_t buffOffset = archiveSize - bytesRead;
if( buffOffset > zip64Eocdl->zip64EocdOffset )
{
// we need to read more data
uint32_t size = archiveSize - zip64Eocdl->zip64EocdOffset;
buffer.reset( new char[size] );
uint32_t bytes = 0;
XRootDStatus st = archive.Read( zip64Eocdl->zip64EocdOffset, size, buffer.get(), bytes );
if( !st.IsOK() ) return st;
}
char *zip64EocdBlock = buffer.get() + ( zip64Eocdl->zip64EocdOffset - buffOffset );
signature = reinterpret_cast<uint32_t*>( zip64EocdBlock );
if( *signature != ZIP64_EOCD::zip64EocdSign )
throw ZipHandlerException<AnyObject>( new XRootDStatus( stError, errDataError, errDataError, "ZIP64 End-of-central-directory signature not found." ), 0 );
zip64Eocd = new ZIP64_EOCD( zip64EocdBlock );
eocd->useZip64 = true;
}
/*
else
it is not ZIP64 so we have everything in EOCD
*/
}
uint64_t offset = eocd->useZip64 ? zip64Eocd->cdOffset : eocd->cdOffset;
existingCdSize = eocd->useZip64 ? zip64Eocd->cdSize : eocd->cdSize;
cdBuffer.reset( new char[existingCdSize] );
uint32_t bytes = 0;
XRootDStatus st = archive.Read( offset, existingCdSize, cdBuffer.get(), bytes );
return st;
}
//taken from XrdClZipArchiveReader.cc
bool IsOpen() const
{
return isOpen;
}
// write the central directory and end of central directory record to the archive
void Finalize()
{
writeOffset = eocd->useZip64 ? zip64Eocd->cdOffset : eocd->cdOffset;
// write central directory records
if ( existingCdSize > 0 )
{
XRootDStatus st = archive.Write( writeOffset, existingCdSize, cdBuffer.get() );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
writeOffset += existingCdSize;
}
for ( uint16_t i=0; i<cdRecords.size(); i++)
{
cdRecords[i]->Write( archive, writeOffset );
writeOffset += cdRecords[i]->cdfhSize;
}
// write EOCD, ZIP64EOCD and ZIP64EOCDL
if ( eocd->useZip64 )
{
zip64Eocd->Write( archive, writeOffset );
writeOffset += zip64Eocd->zip64EocdTotalSize;
zip64Eocdl->Write( archive, writeOffset );
writeOffset += ZIP64_EOCDL::zip64EocdlSize;
}
eocd->Write( archive, writeOffset );
}
// write the contents of the buffer to the archive
// must be called after Append() to ensure correct writeOffset
// fileOffset is the offset of the buffer contents in the input file
void WriteFileData( char *buffer, uint32_t size, uint64_t fileOffset )
{
XRootDStatus st = archive.Write( writeOffset + fileOffset, size, buffer );
if( !st.IsOK() ) throw ZipHandlerException<AnyObject>( &st, 0 );
}
// close the archive
void Close()
{
if ( IsOpen() )
{
XRootDStatus st = archive.Close();
if( st.IsOK() )
{
isOpen = false;
buffer.reset();
cdBuffer.reset();
}
else
throw ZipHandlerException<AnyObject>( &st, 0 );
}
}
private:
File &archive;
std::string archiveUrl;
uint64_t archiveSize;
std::vector<CDFH*> cdRecords;
EOCD *eocd;
ZIP64_EOCD *zip64Eocd;
ZIP64_EOCDL *zip64Eocdl;
std::unique_ptr<char[]> buffer;
std::unique_ptr<char[]> cdBuffer;
uint32_t existingCdSize;
uint64_t writeOffset;
bool isOpen;
bool createNew;
};
}
// for testing purposes - not in final API
int OpenInputFile( std::string inputFilename, struct stat &fileInfo )
{
// open input file for reading then stat it
int inputFd = open( inputFilename.c_str(), O_RDONLY );
if ( inputFd == -1 ) throw std::runtime_error("Failed to open input file.");
else if ( fstat( inputFd, &fileInfo ) == -1 ) throw std::runtime_error("Failed to stat input file.");
return inputFd;
}
// an example of how to use the ZipArchive API
// run the executable with arguments: <input filename> <output file url>
int main( int argc, char **argv )
{
std::string inputFilename = "file.txt";
std::string archiveUrl = "root://localhost//tmp/archive.zip";
// crc for file.txt
uint32_t crc = 0x797b4b0e;
if (argc >= 2)
inputFilename = argv[1];
if (argc >= 3)
archiveUrl = argv[2];
struct stat fileInfo;
int inputFd = OpenInputFile( inputFilename, fileInfo );
XrdCl::File *file = new XrdCl::File();
XrdCl::ZipArchive *archive = new XrdCl::ZipArchive( *file, archiveUrl );
archive->Open();
archive->Append( inputFilename, crc, fileInfo.st_size, fileInfo.st_mtime, fileInfo.st_mode );
// write input file data to ZIP archive
uint64_t fileOffset = 0;
uint32_t size = 10240;
char buffer[size];
if ( lseek( inputFd, fileOffset, SEEK_SET ) == -1 ) throw std::runtime_error("Failed to seek input file.");
uint32_t bytesRead = read( inputFd, buffer, size );
if ( bytesRead == XrdCl::ovrflw32 ) throw std::runtime_error("Failed to read input file.");
while( bytesRead != 0 )
{
archive->WriteFileData( buffer, bytesRead, fileOffset );
fileOffset += bytesRead;
if ( lseek( inputFd, fileOffset, SEEK_SET ) == -1 ) throw std::runtime_error("Failed to seek input file.");
bytesRead = read( inputFd, buffer, size );
if ( bytesRead == XrdCl::ovrflw32 ) throw std::runtime_error("Failed to read input file.");
}
if ( close( inputFd ) == -1 ) throw std::runtime_error("Failed to close input file.");
archive->Finalize();
archive->Close();
return 0;
}