@@ -491,24 +491,24 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
491
491
Debug . Assert ( _fileComment . Length <= ushort . MaxValue ) ;
492
492
493
493
// decide if we need the Zip64 extra field:
494
- Zip64ExtraField zip64ExtraField = default ;
494
+ Zip64ExtraField ? zip64ExtraField = null ;
495
495
uint compressedSizeTruncated , uncompressedSizeTruncated , offsetOfLocalHeaderTruncated ;
496
496
497
- bool zip64Needed = false ;
498
-
499
497
if ( AreSizesTooLarge
500
498
#if DEBUG_FORCE_ZIP64
501
499
|| _archive . _forceZip64
502
500
#endif
503
501
)
504
502
{
505
- zip64Needed = true ;
506
503
compressedSizeTruncated = ZipHelper . Mask32Bit ;
507
504
uncompressedSizeTruncated = ZipHelper . Mask32Bit ;
508
505
509
506
// If we have one of the sizes, the other must go in there as speced for LH, but not necessarily for CH, but we do it anyways
510
- zip64ExtraField . CompressedSize = _compressedSize ;
511
- zip64ExtraField . UncompressedSize = _uncompressedSize ;
507
+ zip64ExtraField = new ( )
508
+ {
509
+ CompressedSize = _compressedSize ,
510
+ UncompressedSize = _uncompressedSize
511
+ } ;
512
512
}
513
513
else
514
514
{
@@ -523,27 +523,32 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
523
523
#endif
524
524
)
525
525
{
526
- zip64Needed = true ;
527
526
offsetOfLocalHeaderTruncated = ZipHelper . Mask32Bit ;
528
527
529
528
// If we have one of the sizes, the other must go in there as speced for LH, but not necessarily for CH, but we do it anyways
530
- zip64ExtraField . LocalHeaderOffset = _offsetOfLocalHeader ;
529
+ zip64ExtraField = new ( )
530
+ {
531
+ LocalHeaderOffset = _offsetOfLocalHeader
532
+ } ;
531
533
}
532
534
else
533
535
{
534
536
offsetOfLocalHeaderTruncated = ( uint ) _offsetOfLocalHeader ;
535
537
}
536
538
537
- if ( zip64Needed )
539
+ if ( zip64ExtraField != null )
540
+ {
538
541
VersionToExtractAtLeast ( ZipVersionNeededValues . Zip64 ) ;
542
+ }
543
+
539
544
540
545
// determine if we can fit zip64 extra field and original extra fields all in
541
- int bigExtraFieldLength = ( zip64Needed ? zip64ExtraField . TotalSize : 0 )
546
+ int bigExtraFieldLength = ( zip64ExtraField != null ? zip64ExtraField . TotalSize : 0 )
542
547
+ ( _cdUnknownExtraFields != null ? ZipGenericExtraField . TotalSize ( _cdUnknownExtraFields ) : 0 ) ;
543
548
ushort extraFieldLength ;
544
549
if ( bigExtraFieldLength > ushort . MaxValue )
545
550
{
546
- extraFieldLength = ( ushort ) ( zip64Needed ? zip64ExtraField . TotalSize : 0 ) ;
551
+ extraFieldLength = ( ushort ) ( zip64ExtraField != null ? zip64ExtraField . TotalSize : 0 ) ;
547
552
_cdUnknownExtraFields = null ;
548
553
}
549
554
else
@@ -555,7 +560,7 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
555
560
{
556
561
long centralDirectoryHeaderLength = ZipCentralDirectoryFileHeader . FieldLocations . DynamicData
557
562
+ _storedEntryNameBytes . Length
558
- + ( zip64Needed ? zip64ExtraField . TotalSize : 0 )
563
+ + ( zip64ExtraField != null ? zip64ExtraField . TotalSize : 0 )
559
564
+ ( _cdUnknownExtraFields != null ? ZipGenericExtraField . TotalSize ( _cdUnknownExtraFields ) : 0 )
560
565
+ _fileComment . Length ;
561
566
@@ -604,14 +609,18 @@ internal void WriteCentralDirectoryFileHeader(bool forceWrite)
604
609
_archive . ArchiveStream . Write ( cdStaticHeader ) ;
605
610
_archive . ArchiveStream . Write ( _storedEntryNameBytes ) ;
606
611
607
- // write extra fields
608
- if ( zip64Needed )
609
- zip64ExtraField . WriteBlock ( _archive . ArchiveStream ) ;
612
+ // write extra fields, and only write zip64ExtraField if we decided we need it (it's not null)
613
+ zip64ExtraField ? . WriteBlock ( _archive . ArchiveStream ) ;
614
+
610
615
if ( _cdUnknownExtraFields != null )
616
+ {
611
617
ZipGenericExtraField . WriteAllBlocks ( _cdUnknownExtraFields , _archive . ArchiveStream ) ;
618
+ }
612
619
613
620
if ( _fileComment . Length > 0 )
621
+ {
614
622
_archive . ArchiveStream . Write ( _fileComment ) ;
623
+ }
615
624
}
616
625
}
617
626
@@ -908,8 +917,7 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
908
917
Debug . Assert ( _storedEntryNameBytes . Length <= ushort . MaxValue ) ;
909
918
910
919
// decide if we need the Zip64 extra field:
911
- Zip64ExtraField zip64ExtraField = default ;
912
- bool zip64Used = false ;
920
+ Zip64ExtraField ? zip64ExtraField = null ;
913
921
uint compressedSizeTruncated , uncompressedSizeTruncated ;
914
922
915
923
// save offset
@@ -932,7 +940,6 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
932
940
if ( _archive . Mode == ZipArchiveMode . Create && _archive . ArchiveStream . CanSeek == false )
933
941
{
934
942
_generalPurposeBitFlag |= BitFlagValues . DataDescriptor ;
935
- zip64Used = false ;
936
943
compressedSizeTruncated = 0 ;
937
944
uncompressedSizeTruncated = 0 ;
938
945
// the crc should not have been set if we are in create mode, but clear it just to be sure
@@ -948,19 +955,20 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
948
955
#endif
949
956
)
950
957
{
951
- zip64Used = true ;
952
958
compressedSizeTruncated = ZipHelper . Mask32Bit ;
953
959
uncompressedSizeTruncated = ZipHelper . Mask32Bit ;
954
960
955
961
// prepare Zip64 extra field object. If we have one of the sizes, the other must go in there
956
- zip64ExtraField . CompressedSize = _compressedSize ;
957
- zip64ExtraField . UncompressedSize = _uncompressedSize ;
962
+ zip64ExtraField = new ( )
963
+ {
964
+ CompressedSize = _compressedSize ,
965
+ UncompressedSize = _uncompressedSize ,
966
+ } ;
958
967
959
968
VersionToExtractAtLeast ( ZipVersionNeededValues . Zip64 ) ;
960
969
}
961
970
else
962
971
{
963
- zip64Used = false ;
964
972
compressedSizeTruncated = ( uint ) _compressedSize ;
965
973
uncompressedSizeTruncated = ( uint ) _uncompressedSize ;
966
974
}
@@ -971,12 +979,12 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
971
979
_offsetOfLocalHeader = _archive . ArchiveStream . Position ;
972
980
973
981
// calculate extra field. if zip64 stuff + original extraField aren't going to fit, dump the original extraField, because this is more important
974
- int bigExtraFieldLength = ( zip64Used ? zip64ExtraField . TotalSize : 0 )
982
+ int bigExtraFieldLength = ( zip64ExtraField != null ? zip64ExtraField . TotalSize : 0 )
975
983
+ ( _lhUnknownExtraFields != null ? ZipGenericExtraField . TotalSize ( _lhUnknownExtraFields ) : 0 ) ;
976
984
ushort extraFieldLength ;
977
985
if ( bigExtraFieldLength > ushort . MaxValue )
978
986
{
979
- extraFieldLength = ( ushort ) ( zip64Used ? zip64ExtraField . TotalSize : 0 ) ;
987
+ extraFieldLength = ( ushort ) ( zip64ExtraField != null ? zip64ExtraField . TotalSize : 0 ) ;
980
988
_lhUnknownExtraFields = null ;
981
989
}
982
990
else
@@ -990,7 +998,7 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
990
998
{
991
999
_archive . ArchiveStream . Seek ( ZipLocalFileHeader . SizeOfLocalHeader + _storedEntryNameBytes . Length , SeekOrigin . Current ) ;
992
1000
993
- if ( zip64Used )
1001
+ if ( zip64ExtraField != null )
994
1002
{
995
1003
_archive . ArchiveStream . Seek ( zip64ExtraField . TotalSize , SeekOrigin . Current ) ;
996
1004
}
@@ -1018,13 +1026,14 @@ private bool WriteLocalFileHeader(bool isEmptyFile, bool forceWrite)
1018
1026
1019
1027
_archive . ArchiveStream . Write ( _storedEntryNameBytes ) ;
1020
1028
1021
- if ( zip64Used )
1022
- zip64ExtraField . WriteBlock ( _archive . ArchiveStream ) ;
1029
+ // Only when handling zip64
1030
+ zip64ExtraField ? . WriteBlock ( _archive . ArchiveStream ) ;
1031
+
1023
1032
if ( _lhUnknownExtraFields != null )
1024
1033
ZipGenericExtraField . WriteAllBlocks ( _lhUnknownExtraFields , _archive . ArchiveStream ) ;
1025
1034
}
1026
1035
1027
- return zip64Used ;
1036
+ return zip64ExtraField != null ;
1028
1037
}
1029
1038
1030
1039
private void WriteLocalFileHeaderAndDataIfNeeded ( bool forceWrite )
0 commit comments