ZipArchive.st
changeset 2511 237dad762f68
parent 2492 128237b8b7b6
child 2512 f1f789f85b34
equal deleted inserted replaced
2510:661f5e28cd6d 2511:237dad762f68
    11 "
    11 "
    12 "{ Package: 'stx:libbasic2' }"
    12 "{ Package: 'stx:libbasic2' }"
    13 
    13 
    14 Object subclass:#ZipArchive
    14 Object subclass:#ZipArchive
    15 	instanceVariableNames:'file mode archiveName firstEntry lastEntry centralDirectory
    15 	instanceVariableNames:'file mode archiveName firstEntry lastEntry centralDirectory
    16 		startOfArchive endOfArchive'
    16 		startOfArchive endOfArchive zipMembersByName'
    17 	classVariableNames:'Lobby RecentlyUsedZipArchives FlushBlock ECREC_SIZE LREC_SIZE
    17 	classVariableNames:'Lobby RecentlyUsedZipArchives FlushBlock ZipFileFormatErrorSignal
    18 		CREC_SIZE SIZE_CENTRAL_DIRECTORY TOTAL_ENTRIES_CENTRAL_DIR
    18 		UnsupportedZipFileFormatErrorSignal'
    19 		C_COMPRESSED_SIZE C_RELATIVE_OFFSET_LOCAL_HEADER
    19 	poolDictionaries:'ZipArchiveConstants'
    20 		C_FILENAME_LENGTH C_UNCOMPRESSED_SIZE C_CENTRALHEADERSIGNATURE
       
    21 		C_LOCALHEADERSIGNATURE C_CENTRALENDSIGNATURE
       
    22 		ZipFileFormatErrorSignal UnsupportedZipFileFormatErrorSignal
       
    23 		COMPR_STORED COMPR_SHRUNK COMPR_REDUCED1 COMPR_REDUCED2
       
    24 		COMPR_REDUCED3 COMPR_REDUCED4 COMPR_IMPLODED COMPR_TOKENIZED
       
    25 		COMPR_DEFLATED EXTERNALFILEATTRIBUTES_ISFILE
       
    26 		EXTERNALFILEATTRIBUTES_ISDIRECTORY'
       
    27 	poolDictionaries:''
       
    28 	category:'System-Support-FileFormats'
    20 	category:'System-Support-FileFormats'
    29 !
    21 !
    30 
    22 
    31 PeekableStream subclass:#AbstractZipStream
    23 PeekableStream subclass:#AbstractZipStream
    32 	instanceVariableNames:'zipArchive zipEntry zipFileStream compressingStream crc32
    24 	instanceVariableNames:'zipArchive zipEntry zipFileStream compressingStream crc32
    53 		compressedSize uncompressedSize fileNameLength extraFieldLength
    45 		compressedSize uncompressedSize fileNameLength extraFieldLength
    54 		fileCommentLength diskNumberStart internalFileAttributes
    46 		fileCommentLength diskNumberStart internalFileAttributes
    55 		externalFileAttributes relativeLocalHeaderOffset fileName
    47 		externalFileAttributes relativeLocalHeaderOffset fileName
    56 		extraField fileComment dataStart data'
    48 		extraField fileComment dataStart data'
    57 	classVariableNames:''
    49 	classVariableNames:''
    58 	poolDictionaries:''
    50 	poolDictionaries:'ZipArchiveConstants'
    59 	privateIn:ZipArchive
    51 	privateIn:ZipArchive
    60 !
    52 !
    61 
    53 
    62 ZipArchive::AbstractZipStream subclass:#ZipReadStream
    54 ZipArchive::AbstractZipStream subclass:#ZipReadStream
    63 	instanceVariableNames:'readPosition peek'
    55 	instanceVariableNames:'readPosition peek'
    64 	classVariableNames:''
    56 	classVariableNames:''
    65 	poolDictionaries:''
    57 	poolDictionaries:'ZipArchiveConstants'
    66 	privateIn:ZipArchive
    58 	privateIn:ZipArchive
    67 !
    59 !
    68 
    60 
    69 ZipArchive::AbstractZipStream subclass:#ZipWriteStream
    61 ZipArchive::AbstractZipStream subclass:#ZipWriteStream
    70 	instanceVariableNames:''
    62 	instanceVariableNames:''
    71 	classVariableNames:''
    63 	classVariableNames:''
    72 	poolDictionaries:''
    64 	poolDictionaries:'ZipArchiveConstants'
    73 	privateIn:ZipArchive
    65 	privateIn:ZipArchive
    74 !
    66 !
    75 
    67 
    76 !ZipArchive primitiveDefinitions!
    68 !ZipArchive primitiveDefinitions!
    77 %{
    69 %{
  1041 !
  1033 !
  1042 
  1034 
  1043 documentation
  1035 documentation
  1044 "
  1036 "
  1045     provides access to a zip archive.
  1037     provides access to a zip archive.
  1046     Caveat: the only compression method (for now) is deflate.
  1038     Caveat: 
       
  1039         the only compression methods (for now) are store and deflate.
  1047 
  1040 
  1048     [author:]
  1041     [author:]
  1049         Claus Gittinger
  1042         Claus Gittinger
  1050 "
  1043 "
  1051 !
  1044 !
  2939 ! !
  2932 ! !
  2940 
  2933 
  2941 !ZipArchive class methodsFor:'class initialization'!
  2934 !ZipArchive class methodsFor:'class initialization'!
  2942 
  2935 
  2943 initialize
  2936 initialize
  2944     ECREC_SIZE := 18.
       
  2945     LREC_SIZE := 26.
       
  2946     CREC_SIZE := 42.
       
  2947 
       
  2948     TOTAL_ENTRIES_CENTRAL_DIR := 10.
       
  2949     SIZE_CENTRAL_DIRECTORY := 12.
       
  2950 
       
  2951     C_COMPRESSED_SIZE := 16.
       
  2952     C_UNCOMPRESSED_SIZE := 20.
       
  2953     C_FILENAME_LENGTH := 24.
       
  2954     C_RELATIVE_OFFSET_LOCAL_HEADER := 38.
       
  2955 
       
  2956     C_LOCALHEADERSIGNATURE := 16r04034b50.
       
  2957     C_CENTRALHEADERSIGNATURE := 16r02014b50.
       
  2958     C_CENTRALENDSIGNATURE := 16r06054b50.
       
  2959 
       
  2960     EXTERNALFILEATTRIBUTES_ISFILE := 32.
       
  2961     EXTERNALFILEATTRIBUTES_ISDIRECTORY := 16.
       
  2962 
       
  2963     "/ compression methods
       
  2964     COMPR_STORED          :=  0.
       
  2965     COMPR_SHRUNK          :=  1.
       
  2966     COMPR_REDUCED1        :=  2.
       
  2967     COMPR_REDUCED2        :=  3.
       
  2968     COMPR_REDUCED3        :=  4.
       
  2969     COMPR_REDUCED4        :=  5.
       
  2970     COMPR_IMPLODED        :=  6.
       
  2971     COMPR_TOKENIZED       :=  7.
       
  2972     COMPR_DEFLATED        :=  8.
       
  2973 
       
  2974     ZipFileFormatErrorSignal isNil ifTrue:[
  2937     ZipFileFormatErrorSignal isNil ifTrue:[
  2975         ZipFileFormatErrorSignal := OpenError newSignalMayProceed:true.
  2938         ZipFileFormatErrorSignal := OpenError newSignalMayProceed:true.
  2976         ZipFileFormatErrorSignal nameClass:self message:#zipFileFormatErrorSignal.
  2939         ZipFileFormatErrorSignal nameClass:self message:#zipFileFormatErrorSignal.
  2977         ZipFileFormatErrorSignal notifierString:'unrecognized/bad zip file format'.
  2940         ZipFileFormatErrorSignal notifierString:'unrecognized/bad zip file format'.
  2978 
  2941 
  2987 
  2950 
  2988     "
  2951     "
  2989      self initialize
  2952      self initialize
  2990     "
  2953     "
  2991 
  2954 
  2992     "Modified: / 29.3.1998 / 20:17:18 / cg"
  2955     "Modified: / 19-11-2010 / 15:44:28 / cg"
  2993 ! !
  2956 ! !
  2994 
  2957 
  2995 !ZipArchive class methodsFor:'cleanup'!
  2958 !ZipArchive class methodsFor:'cleanup'!
  2996 
  2959 
  2997 flush
  2960 flush
  3034     "Modified: / 7.4.1998 / 17:58:57 / cg"
  2997     "Modified: / 7.4.1998 / 17:58:57 / cg"
  3035 ! !
  2998 ! !
  3036 
  2999 
  3037 !ZipArchive class methodsFor:'constants'!
  3000 !ZipArchive class methodsFor:'constants'!
  3038 
  3001 
       
  3002 COMPRESSION_DEFLATED
       
  3003     <resource: #obsolete>
       
  3004 
       
  3005     "please use compressionDeflated instead (Squeak compat.)"
       
  3006 
       
  3007     self obsoleteMethodWarning.
       
  3008     ^ COMPRESSION_DEFLATED
       
  3009 
       
  3010     "Created: / 19-11-2010 / 15:40:38 / cg"
       
  3011 !
       
  3012 
  3039 COMPR_DEFLATED
  3013 COMPR_DEFLATED
       
  3014     <resource: #obsolete>
       
  3015 
  3040     "please use compressionDeflated instead (Squeak compat.)"
  3016     "please use compressionDeflated instead (Squeak compat.)"
  3041 
  3017 
  3042     ^ COMPR_DEFLATED
  3018     self obsoleteMethodWarning.
  3043 !
  3019     ^ COMPRESSION_DEFLATED
  3044 
  3020 
  3045 COMPR_STORED
  3021     "Modified: / 19-11-2010 / 15:58:28 / cg"
  3046     "please use compressionStored instead (Squeak compat.)"
       
  3047 
       
  3048     ^ COMPR_DEFLATED
       
  3049 !
  3022 !
  3050 
  3023 
  3051 LREC_SIZE
  3024 LREC_SIZE
  3052     ^ LREC_SIZE
  3025     ^ LREC_SIZE
  3053 
  3026 
  3059 
  3032 
  3060     "Created: / 29.3.1998 / 19:11:20 / cg"
  3033     "Created: / 29.3.1998 / 19:11:20 / cg"
  3061 !
  3034 !
  3062 
  3035 
  3063 compressionDeflated
  3036 compressionDeflated
  3064     "same as COMPR_DEFLATED - squeak compatibility"
  3037     "same as COMPRESSION_DEFLATED - squeak compatibility"
  3065 
  3038 
  3066     ^ COMPR_DEFLATED
  3039     ^ COMPRESSION_DEFLATED
       
  3040 
       
  3041     "Modified: / 19-11-2010 / 15:58:39 / cg"
  3067 !
  3042 !
  3068 
  3043 
  3069 compressionStored
  3044 compressionStored
  3070     "same as COMPR_STORED - squeak compatibility"
  3045     "same as COMPRESSION_STORED - squeak compatibility"
  3071 
  3046 
  3072     ^ COMPR_STORED
  3047     ^ COMPRESSION_STORED
       
  3048 
       
  3049     "Modified: / 19-11-2010 / 15:59:00 / cg"
  3073 !
  3050 !
  3074 
  3051 
  3075 streamBufferSize
  3052 streamBufferSize
  3076     ^ 65536     "/ 1024 * 64
  3053     ^ 65536     "/ 1024 * 64
  3077 
  3054 
  3397 !ZipArchive methodsFor:'private-decompression'!
  3374 !ZipArchive methodsFor:'private-decompression'!
  3398 
  3375 
  3399 decode:rawBytes method:compressionMethod size:uncompressedSize
  3376 decode:rawBytes method:compressionMethod size:uncompressedSize
  3400     |outBytes|
  3377     |outBytes|
  3401 
  3378 
  3402     compressionMethod == COMPR_STORED ifTrue:[
  3379     compressionMethod == COMPRESSION_STORED ifTrue:[
  3403         "/
  3380         "/
  3404         "/ uncompressed
  3381         "/ uncompressed
  3405         "/
  3382         "/
  3406         ^ rawBytes
  3383         ^ rawBytes
  3407     ].
  3384     ].
  3408 
  3385 
  3409     compressionMethod == COMPR_DEFLATED ifTrue:[
  3386     compressionMethod == COMPRESSION_DEFLATED ifTrue:[
  3410         "/
  3387         "/
  3411         "/ deflate/inflate algorithm
  3388         "/ deflate/inflate algorithm
  3412         "/
  3389         "/
  3413         outBytes := ByteArray new:uncompressedSize.
  3390         outBytes := ByteArray new:uncompressedSize.
  3414         ^ self inflate:rawBytes to:outBytes
  3391         ^ self inflate:rawBytes to:outBytes
  3415     ].
  3392     ].
  3416 
  3393 
  3417     "/
  3394     "/
  3418     "/ the other algorithms are not (yet) supported
  3395     "/ the other algorithms are not (yet) supported
  3419     "/
  3396     "/
  3420     compressionMethod == COMPR_SHRUNK ifTrue:[
  3397     compressionMethod == COMPRESSION_SHRUNK ifTrue:[
  3421         self error:'unsupported compression method: SHRUNK'.
  3398         self error:'unsupported compression method: SHRUNK'.
  3422         ^ nil
  3399         ^ nil
  3423     ].
  3400     ].
  3424     compressionMethod == COMPR_REDUCED1 ifTrue:[
  3401     compressionMethod == COMPRESSION_REDUCED1 ifTrue:[
  3425         self error:'unsupported compression method: REDUCED1'.
  3402         self error:'unsupported compression method: REDUCED1'.
  3426         ^ nil
  3403         ^ nil
  3427     ].
  3404     ].
  3428     compressionMethod == COMPR_REDUCED2 ifTrue:[
  3405     compressionMethod == COMPRESSION_REDUCED2 ifTrue:[
  3429         self error:'unsupported compression method: REDUCED2'.
  3406         self error:'unsupported compression method: REDUCED2'.
  3430         ^ nil
  3407         ^ nil
  3431     ].
  3408     ].
  3432     compressionMethod == COMPR_REDUCED3 ifTrue:[
  3409     compressionMethod == COMPRESSION_REDUCED3 ifTrue:[
  3433         self error:'unsupported compression method: REDUCED3'.
  3410         self error:'unsupported compression method: REDUCED3'.
  3434         ^ nil
  3411         ^ nil
  3435     ].
  3412     ].
  3436     compressionMethod == COMPR_REDUCED4 ifTrue:[
  3413     compressionMethod == COMPRESSION_REDUCED4 ifTrue:[
  3437         self error:'unsupported compression method: REDUCED4'.
  3414         self error:'unsupported compression method: REDUCED4'.
  3438         ^ nil
  3415         ^ nil
  3439     ].
  3416     ].
  3440     compressionMethod == COMPR_IMPLODED ifTrue:[
  3417     compressionMethod == COMPRESSION_IMPLODED ifTrue:[
  3441         self error:'unsupported compression method: IMPLODED'.
  3418         self error:'unsupported compression method: IMPLODED'.
  3442         ^ nil
  3419         ^ nil
  3443     ].
  3420     ].
  3444     compressionMethod == COMPR_TOKENIZED ifTrue:[
  3421     compressionMethod == COMPRESSION_TOKENIZED ifTrue:[
  3445         self error:'unsupported compression method: TOKENIZED'.
  3422         self error:'unsupported compression method: TOKENIZED'.
  3446         ^ nil
  3423         ^ nil
  3447     ].
  3424     ].
  3448 
  3425 
  3449     self error:'unsupported compression method'.
  3426     self error:'unsupported compression method'.
  3450     ^ nil
  3427     ^ nil
  3451 
  3428 
  3452     "Created: / 29.3.1998 / 20:14:45 / cg"
  3429     "Created: / 29-03-1998 / 20:14:45 / cg"
  3453     "Modified: / 8.4.1998 / 10:31:34 / cg"
  3430     "Modified: / 19-11-2010 / 15:39:49 / cg"
  3454 !
  3431 !
  3455 
  3432 
  3456 inflate:inBytes to:outBytes
  3433 inflate:inBytes to:outBytes
  3457     |inflateReturnCode|
  3434     |inflateReturnCode|
  3458 
  3435 
  3497 
  3474 
  3498     centralDirectory centralDirectoryStartOffset: file position.
  3475     centralDirectory centralDirectoryStartOffset: file position.
  3499 
  3476 
  3500     [ zipEntry notNil ] whileTrue: [
  3477     [ zipEntry notNil ] whileTrue: [
  3501         noEntries := noEntries + 1.
  3478         noEntries := noEntries + 1.
  3502         file nextPutLong: C_CENTRALHEADERSIGNATURE MSB:false.            
  3479         file nextPutLong: C_CENTRAL_HEADER_SIGNATURE MSB:false.            
  3503         file nextPutShort:zipEntry versionMadeBy MSB:false.
  3480         file nextPutShort:zipEntry versionMadeBy MSB:false.
  3504         file nextPutShort:zipEntry versionNeedToExtract MSB:false.
  3481         file nextPutShort:zipEntry versionNeedToExtract MSB:false.
  3505         file nextPutShort:zipEntry generalPurposBitFlag MSB:false.
  3482         file nextPutShort:zipEntry generalPurposBitFlag MSB:false.
  3506         file nextPutShort:zipEntry compressionMethod MSB:false.
  3483         file nextPutShort:zipEntry compressionMethod MSB:false.
  3507         file nextPutShort:zipEntry lastModFileTime MSB:false.
  3484         file nextPutShort:zipEntry lastModFileTime MSB:false.
  3544     file nextPutShort:centralDirectory zipCommentLength MSB:false.
  3521     file nextPutShort:centralDirectory zipCommentLength MSB:false.
  3545 
  3522 
  3546     centralDirectory zipCommentLength ~~ 0 ifTrue: [
  3523     centralDirectory zipCommentLength ~~ 0 ifTrue: [
  3547         file nextPutAll: centralDirectory zipComment.
  3524         file nextPutAll: centralDirectory zipComment.
  3548     ].
  3525     ].
       
  3526 
       
  3527     "Modified: / 19-11-2010 / 15:43:15 / cg"
  3549 !
  3528 !
  3550 
  3529 
  3551 addMember
  3530 addMember
  3552     "add a zipMember"
  3531     "add a zipMember"
  3553 
  3532 
  3600 !
  3579 !
  3601 
  3580 
  3602 findMember:name
  3581 findMember:name
  3603     "find a zipMember by name"
  3582     "find a zipMember by name"
  3604 
  3583 
       
  3584 "/    zipMembersByName isNil ifTrue:[
       
  3585 "/        zipMembersByName := Dictionary new.
       
  3586 "/        self zipMembersDo:[:zipd |
       
  3587 "/            zipMembersByName at:(zipd fileName) put:zipd.
       
  3588 "/        ].
       
  3589 "/    ].
       
  3590 "/    ^ zipMembersByName at:name ifAbsent:nil.
       
  3591 
  3605     self zipMembersDo:[:zipd |
  3592     self zipMembersDo:[:zipd |
  3606         (zipd fileName = name) ifTrue:[^ zipd].
  3593         (zipd fileName = name) ifTrue:[^ zipd].
  3607     ].
  3594     ].
  3608     ^ nil
  3595     ^ nil
  3609 
  3596 
  3610     "Modified: / 30.3.1998 / 17:13:30 / cg"
  3597     "Modified: / 18-11-2010 / 20:23:35 / cg"
  3611 !
  3598 !
  3612 
  3599 
  3613 readDirectory
  3600 readDirectory
  3614     "read the zip directory into a linked-list of zipMembers"
  3601     "read the zip directory into a linked-list of zipMembers"
  3615 
  3602 
  3656 
  3643 
  3657             (file position + (self class centralDirectoryMinimumSize)) > endOfArchive ifTrue: [
  3644             (file position + (self class centralDirectoryMinimumSize)) > endOfArchive ifTrue: [
  3658                 ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - central directory entry out of archive bounds'.
  3645                 ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - central directory entry out of archive bounds'.
  3659             ].
  3646             ].
  3660             centralFileHeaderSignature := file nextLongMSB:false.            
  3647             centralFileHeaderSignature := file nextLongMSB:false.            
  3661             centralFileHeaderSignature ~= C_CENTRALHEADERSIGNATURE ifTrue:[
  3648             centralFileHeaderSignature ~= C_CENTRAL_HEADER_SIGNATURE ifTrue:[
  3662                 ZipFileFormatErrorSignal raiseRequestErrorString:' - file format error - bad centralHeaderSignature in: ' ,
  3649                 ZipFileFormatErrorSignal raiseRequestErrorString:' - file format error - bad centralHeaderSignature in: ' ,
  3663                                                 (file isFileStream ifTrue:[file pathName] ifFalse:['inStream']).
  3650                                                 (file isFileStream ifTrue:[file pathName] ifFalse:['inStream']).
  3664                 ^ self.
  3651                 ^ self.
  3665             ].
  3652             ].
  3666 
  3653 
  3687      ZipArchive flush.
  3674      ZipArchive flush.
  3688      ZipArchive oldFileNamed:'/usr/lib/jdk1.1.7/lib/classes.zip'
  3675      ZipArchive oldFileNamed:'/usr/lib/jdk1.1.7/lib/classes.zip'
  3689      ZipArchive oldFileNamed:'/usr/lib/jdk1.1.8/lib/classes.zip'
  3676      ZipArchive oldFileNamed:'/usr/lib/jdk1.1.8/lib/classes.zip'
  3690     "
  3677     "
  3691 
  3678 
  3692     "Modified: / 19.10.1998 / 21:27:32 / cg"
  3679     "Modified: / 19-11-2010 / 15:43:24 / cg"
  3693 !
  3680 !
  3694 
  3681 
  3695 searchForEndOfCentralDirectorySignature
  3682 searchForEndOfCentralDirectorySignature
  3696     "read the zip directory into a linked-list of zipMembers"
  3683     "read the zip directory into a linked-list of zipMembers"
  3697 
  3684 
  3826                     buffer := ByteArray new: nextBlockSize.
  3813                     buffer := ByteArray new: nextBlockSize.
  3827                 ].
  3814                 ].
  3828             ].
  3815             ].
  3829 
  3816 
  3830             nextBlockSize > 0 ifTrue: [
  3817             nextBlockSize > 0 ifTrue: [
  3831                 zmemb compressionMethod == COMPR_DEFLATED ifTrue:[
  3818                 zmemb compressionMethod == COMPRESSION_DEFLATED ifTrue:[
  3832                     myZipStream isNil ifTrue: [
  3819                     myZipStream isNil ifTrue: [
  3833                         file binary.
  3820                         file binary.
  3834                         myZipStream := ZipStream readOpenAsZipStreamOn: file.
  3821                         myZipStream := ZipStream readOpenAsZipStreamOn: file.
  3835                     ].
  3822                     ].
  3836                     buffer := myZipStream next:nextBlockSize.
  3823                     buffer := myZipStream next:nextBlockSize.
  3837                 ] ifFalse:[
  3824                 ] ifFalse:[
  3838                     zmemb compressionMethod == COMPR_STORED ifTrue:[
  3825                     zmemb compressionMethod == COMPRESSION_STORED ifTrue:[
  3839                         file nextBytes:nextBlockSize into:buffer startingAt:1.
  3826                         file nextBytes:nextBlockSize into:buffer startingAt:1.
  3840                     ] ifFalse:[
  3827                     ] ifFalse:[
  3841                         UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
  3828                         UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
  3842                     ].
  3829                     ].
  3843                 ].
  3830                 ].
  3851             myZipStream close.
  3838             myZipStream close.
  3852         ].
  3839         ].
  3853     ].
  3840     ].
  3854 
  3841 
  3855     ^ true.
  3842     ^ true.
       
  3843 
       
  3844     "Modified: / 19-11-2010 / 15:58:24 / cg"
  3856 !
  3845 !
  3857 
  3846 
  3858 restoreOsDirectory:osDirectoryName fromArchiveDirectory: archiveDirectoryName
  3847 restoreOsDirectory:osDirectoryName fromArchiveDirectory: archiveDirectoryName
  3859     |osDirectory directoryAlreadyCreated archiveDirectoryNameSize|
  3848     |osDirectory directoryAlreadyCreated archiveDirectoryNameSize|
  3860 
  3849 
  3992         ].
  3981         ].
  3993     ].
  3982     ].
  3994 !
  3983 !
  3995 
  3984 
  3996 addArchiveDirectoryCompressed: archiveDirectoryName fromOsDirectory: osDirectoryName
  3985 addArchiveDirectoryCompressed: archiveDirectoryName fromOsDirectory: osDirectoryName
  3997     ^ self addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName compressMethod:COMPR_DEFLATED 
  3986     ^ self addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName compressMethod:COMPRESSION_DEFLATED
       
  3987 
       
  3988     "Modified: / 19-11-2010 / 15:58:04 / cg"
  3998 !
  3989 !
  3999 
  3990 
  4000 addDirectory: aDirectoryName
  3991 addDirectory: aDirectoryName
  4001     "do not create directories (isDirectory = true) - they are not compatible between operating systems"
  3992     "do not create directories (isDirectory = true) - they are not compatible between operating systems"
  4002 
  3993 
  4003     <resource: #obsolete>
  3994     <resource: #obsolete>
  4004 
  3995 
  4005     ^ self addFile:aDirectoryName withContents:nil compressMethod:COMPR_STORED asDirectory:true.
  3996     ^ self addFile:aDirectoryName withContents:nil compressMethod:COMPRESSION_STORED asDirectory:true.
       
  3997 
       
  3998     "Modified: / 19-11-2010 / 15:38:59 / cg"
  4006 !
  3999 !
  4007 
  4000 
  4008 addFile: aFileName fromStream: aStream
  4001 addFile: aFileName fromStream: aStream
  4009     ^ self addFile: aFileName fromStream: aStream compressMethod:COMPR_STORED asDirectory:false
  4002     ^ self addFile: aFileName fromStream: aStream compressMethod:COMPRESSION_STORED asDirectory:false
       
  4003 
       
  4004     "Modified: / 19-11-2010 / 15:39:02 / cg"
  4010 !
  4005 !
  4011 
  4006 
  4012 addFile:aFileName fromStream:aStream compressMethod: theCompressMethodArg
  4007 addFile:aFileName fromStream:aStream compressMethod: theCompressMethodArg
  4013     ^ self addFile:aFileName fromStream:aStream compressMethod:theCompressMethodArg asDirectory:false
  4008     ^ self addFile:aFileName fromStream:aStream compressMethod:theCompressMethodArg asDirectory:false
  4014 !
  4009 !
  4023         ^ self error: 'ZipArchive not open for writing ...'.
  4018         ^ self error: 'ZipArchive not open for writing ...'.
  4024     ].
  4019     ].
  4025 
  4020 
  4026     theCompressMethod := theCompressMethodArg.
  4021     theCompressMethod := theCompressMethodArg.
  4027 
  4022 
  4028     ((theCompressMethod == COMPR_DEFLATED) 
  4023     ((theCompressMethod == COMPRESSION_DEFLATED) 
  4029     or:[ theCompressMethod == COMPR_STORED ]) ifFalse:[
  4024     or:[ theCompressMethod == COMPRESSION_STORED ]) ifFalse:[
  4030         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4025         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4031         "/ if proceeded, write as uncompressed
  4026         "/ if proceeded, write as uncompressed
  4032         theCompressMethod := COMPR_STORED
  4027         theCompressMethod := COMPRESSION_STORED
  4033     ].
  4028     ].
  4034 
  4029 
  4035     zipEntry := ZipMember new default.
  4030     zipEntry := ZipMember new default.
  4036     self addMember:zipEntry.
  4031     self addMember:zipEntry.
  4037 
  4032 
  4040     zipEntry fileName: theZipFileName.
  4035     zipEntry fileName: theZipFileName.
  4041     zipEntry fileNameLength: theZipFileName size.
  4036     zipEntry fileNameLength: theZipFileName size.
  4042     zipEntry uncompressedSize: 0.
  4037     zipEntry uncompressedSize: 0.
  4043 
  4038 
  4044     isDirectory ifTrue: [
  4039     isDirectory ifTrue: [
  4045         theCompressMethod := COMPR_STORED.
  4040         theCompressMethod := COMPRESSION_STORED.
  4046         zipEntry externalFileAttributes: EXTERNALFILEATTRIBUTES_ISDIRECTORY.
  4041         zipEntry externalFileAttributes: EXTERNALFILEATTRIBUTES_ISDIRECTORY.
  4047     ] ifFalse: [
  4042     ] ifFalse: [
  4048         zipEntry compressionMethod: theCompressMethod.
  4043         zipEntry compressionMethod: theCompressMethod.
  4049         zipEntry internalFileAttributes: 1.
  4044         zipEntry internalFileAttributes: 1.
  4050         zipEntry externalFileAttributes: EXTERNALFILEATTRIBUTES_ISFILE.
  4045         zipEntry externalFileAttributes: EXTERNALFILEATTRIBUTES_ISFILE.
  4072             nextBlockSize := aStream nextBytes:streamBufferSize into:buffer startingAt:1.
  4067             nextBlockSize := aStream nextBytes:streamBufferSize into:buffer startingAt:1.
  4073 
  4068 
  4074             nextBlockSize > 0 ifTrue: [
  4069             nextBlockSize > 0 ifTrue: [
  4075                 unCompressedDataSize := unCompressedDataSize + nextBlockSize.
  4070                 unCompressedDataSize := unCompressedDataSize + nextBlockSize.
  4076                 crc32 := ZipStream crc32BytesIn: buffer from:1 to:nextBlockSize crc:crc32.
  4071                 crc32 := ZipStream crc32BytesIn: buffer from:1 to:nextBlockSize crc:crc32.
  4077                 theCompressMethod == COMPR_DEFLATED ifTrue: [
  4072                 theCompressMethod == COMPRESSION_DEFLATED ifTrue: [
  4078                     myZipStream isNil ifTrue: [
  4073                     myZipStream isNil ifTrue: [
  4079                         myZipStream := ZipStream writeOpenAsZipStreamOn:file.
  4074                         myZipStream := ZipStream writeOpenAsZipStreamOn:file.
  4080                     ].
  4075                     ].
  4081                     myZipStream nextPutBytes:nextBlockSize from:buffer startingAt:1.
  4076                     myZipStream nextPutBytes:nextBlockSize from:buffer startingAt:1.
  4082                 ] ifFalse: [theCompressMethod == COMPR_STORED ifTrue: [
  4077                 ] ifFalse: [theCompressMethod == COMPRESSION_STORED ifTrue: [
  4083                     file nextPutBytes:nextBlockSize from:buffer startingAt:1.
  4078                     file nextPutBytes:nextBlockSize from:buffer startingAt:1.
  4084                 ] ifFalse:[
  4079                 ] ifFalse:[
  4085                     UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4080                     UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4086                 ]].
  4081                 ]].
  4087             ].
  4082             ].
  4098     zipEntry crc32:crc32.
  4093     zipEntry crc32:crc32.
  4099     zipEntry uncompressedSize: unCompressedDataSize.
  4094     zipEntry uncompressedSize: unCompressedDataSize.
  4100 
  4095 
  4101     zipEntry rewriteCrcAndSizeTo:file.
  4096     zipEntry rewriteCrcAndSizeTo:file.
  4102     file setToEnd.
  4097     file setToEnd.
       
  4098 
       
  4099     "Modified: / 19-11-2010 / 15:39:32 / cg"
  4103 !
  4100 !
  4104 
  4101 
  4105 addFile: aFileName withContents: data
  4102 addFile: aFileName withContents: data
  4106     ^ self addFile: aFileName withContents: data compressMethod:COMPR_STORED asDirectory: false.
  4103     ^ self addFile: aFileName withContents: data compressMethod:COMPRESSION_STORED asDirectory: false.
       
  4104 
       
  4105     "Modified: / 19-11-2010 / 15:39:13 / cg"
  4107 !
  4106 !
  4108 
  4107 
  4109 addFile:aFileName withContents:data compressMethod:theCompressMethodArg asDirectory:isDirectory
  4108 addFile:aFileName withContents:data compressMethod:theCompressMethodArg asDirectory:isDirectory
  4110     "do not create directories (isDirectory = true) - they are not compatible between operating systems"
  4109     "do not create directories (isDirectory = true) - they are not compatible between operating systems"
  4111 
  4110 
       
  4111     ^ self basicAddFile:aFileName withContents:data compressMethod:theCompressMethodArg asDirectory:isDirectory
       
  4112 
       
  4113     "Modified: / 18-11-2010 / 19:31:36 / cg"
       
  4114 !
       
  4115 
       
  4116 addFileCompressed: aFileName fromStream: aStream
       
  4117     ^ self addFile: aFileName fromStream: aStream compressMethod: COMPRESSION_DEFLATED asDirectory:false
       
  4118 
       
  4119     "Modified: / 19-11-2010 / 15:58:07 / cg"
       
  4120 !
       
  4121 
       
  4122 addFileCompressed: aFileName withContents: data
       
  4123     ^ self addFile: aFileName withContents: data compressMethod: COMPRESSION_DEFLATED asDirectory: false.
       
  4124 
       
  4125     "Modified: / 19-11-2010 / 15:58:10 / cg"
       
  4126 !
       
  4127 
       
  4128 addString: aString as: path
       
  4129     ^ self 
       
  4130         addFile: path fromStream: (aString readStream)
       
  4131 !
       
  4132 
       
  4133 basicAddFile:aFileName withContents:data compressMethod:theCompressMethodArg asDirectory:isDirectory
       
  4134     "do not create directories (isDirectory = true) - they are not compatible between operating systems"
       
  4135 
  4112     |zipEntry theCompressedData curTime curDate theZipFileName theCompressMethod|
  4136     |zipEntry theCompressedData curTime curDate theZipFileName theCompressMethod|
  4113 
  4137 
       
  4138 (aFileName startsWith:'Project') ifFalse:[
       
  4139 (aFileName startsWith:'Doc') ifFalse:[
       
  4140 self halt
       
  4141 ].
       
  4142 ].
  4114     (file isNil or: [mode ~~ #write]) ifTrue: [
  4143     (file isNil or: [mode ~~ #write]) ifTrue: [
  4115         ^ self error: 'ZipArchive not open for writing ...'.
  4144         ^ self error: 'ZipArchive not open for writing ...'.
  4116     ].
  4145     ].
  4117 
  4146 
  4118     theCompressMethod := theCompressMethodArg.
  4147     theCompressMethod := theCompressMethodArg.
  4119     ((theCompressMethod == COMPR_DEFLATED) 
  4148     ((theCompressMethod == COMPRESSION_DEFLATED) 
  4120      or:[ theCompressMethod == COMPR_STORED ]) ifFalse:[
  4149      or:[ theCompressMethod == COMPRESSION_STORED ]) ifFalse:[
  4121         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4150         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4122         "/ if proceeded, write as uncompressed
  4151         "/ if proceeded, write as uncompressed
  4123         theCompressMethod := COMPR_STORED
  4152         theCompressMethod := COMPRESSION_STORED
  4124     ].
  4153     ].
  4125 
  4154 
  4126     zipEntry := ZipMember new default.
  4155     zipEntry := ZipMember new default.
  4127     self addMember:zipEntry.
  4156     self addMember:zipEntry.
  4128 
  4157 
  4149     data notEmptyOrNil ifTrue: [     
  4178     data notEmptyOrNil ifTrue: [     
  4150         "/ crc32 is allways reqired (not as written in docu to be zero in case of uncompressed mode)
  4179         "/ crc32 is allways reqired (not as written in docu to be zero in case of uncompressed mode)
  4151         zipEntry crc32: (ZipStream crc32BytesIn: data).
  4180         zipEntry crc32: (ZipStream crc32BytesIn: data).
  4152     ].
  4181     ].
  4153 
  4182 
  4154     (isDirectory not and: [theCompressMethod == COMPR_DEFLATED]) ifTrue: [
  4183     (isDirectory not and: [theCompressMethod == COMPRESSION_DEFLATED]) ifTrue: [
  4155         |tmpCompressedData tmpCompressedDataSize|
  4184         |tmpCompressedData tmpCompressedDataSize|
  4156         tmpCompressedData := ByteArray new:(data size + 16). "/ if the compression is less then the additional overhead we need more space in buffer
  4185         tmpCompressedData := ByteArray new:(data size + 16). "/ if the compression is less then the additional overhead we need more space in buffer
  4157         tmpCompressedDataSize := ZipStream compress:data into:tmpCompressedData.
  4186         tmpCompressedDataSize := ZipStream compress:data into:tmpCompressedData.
  4158 
  4187 
  4159         zipEntry compressedSize: (tmpCompressedDataSize - 6). "/6 = the zlib specific data 2 bytes in front and 4 bytes behind the real data
  4188         zipEntry compressedSize: (tmpCompressedDataSize - 6). "/6 = the zlib specific data 2 bytes in front and 4 bytes behind the real data
  4160         theCompressedData := tmpCompressedData copyFrom: 3. "/ 2 bytes before the real data
  4189         theCompressedData := tmpCompressedData copyFrom: 3. "/ 2 bytes before the real data
  4161     ] ifFalse: [
  4190     ] ifFalse: [
  4162         theCompressMethod == COMPR_STORED ifTrue:[ 
  4191         theCompressMethod == COMPRESSION_STORED ifTrue:[ 
  4163             zipEntry compressedSize: zipEntry uncompressedSize.
  4192             zipEntry compressedSize: zipEntry uncompressedSize.
  4164             theCompressedData := data.
  4193             theCompressedData := data.
  4165         ] ifFalse:[
  4194         ] ifFalse:[
  4166             self error "/ cannot happen
  4195             self error "/ cannot happen
  4167         ].
  4196         ].
  4173     zipEntry writeTo:file.
  4202     zipEntry writeTo:file.
  4174 
  4203 
  4175     theCompressedData notNil ifTrue: [
  4204     theCompressedData notNil ifTrue: [
  4176         file nextPutBytes: zipEntry compressedSize from: theCompressedData.
  4205         file nextPutBytes: zipEntry compressedSize from: theCompressedData.
  4177     ].
  4206     ].
  4178 !
  4207 
  4179 
  4208     "Created: / 18-11-2010 / 19:31:10 / cg"
  4180 addFileCompressed: aFileName fromStream: aStream
  4209     "Modified: / 19-11-2010 / 15:39:26 / cg"
  4181     ^ self addFile: aFileName fromStream: aStream compressMethod: COMPR_DEFLATED asDirectory:false
       
  4182 !
       
  4183 
       
  4184 addFileCompressed: aFileName withContents: data
       
  4185     ^ self addFile: aFileName withContents: data compressMethod: COMPR_DEFLATED asDirectory: false.
       
  4186 !
       
  4187 
       
  4188 addString: aString as: path
       
  4189     ^ self 
       
  4190         addFile: path fromStream: (aString readStream)
       
  4191 ! !
  4210 ! !
  4192 
  4211 
  4193 !ZipArchive methodsFor:'writing - stream'!
  4212 !ZipArchive methodsFor:'writing - stream'!
  4194 
  4213 
  4195 compressedWriteStreamFor:nameOfFileInArchive
  4214 compressedWriteStreamFor:nameOfFileInArchive
  4196     "create new entry in central directory"
  4215     "create new entry in central directory"
  4197 
  4216 
  4198     ^ self writeStreamFor:nameOfFileInArchive compressMethod:COMPR_DEFLATED
  4217     ^ self writeStreamFor:nameOfFileInArchive compressMethod:COMPRESSION_DEFLATED
       
  4218 
       
  4219     "Modified: / 19-11-2010 / 15:58:13 / cg"
  4199 !
  4220 !
  4200 
  4221 
  4201 writeStreamFor:nameOfFileInArchive compressMethod:theCompressMethodArg
  4222 writeStreamFor:nameOfFileInArchive compressMethod:theCompressMethodArg
  4202     "create new entry in central directory"
  4223     "create new entry in central directory"
  4203 
  4224 
  4207         ^ self error: 'ZipArchive not open for writing ...'.
  4228         ^ self error: 'ZipArchive not open for writing ...'.
  4208     ].
  4229     ].
  4209 
  4230 
  4210     theCompressMethod := theCompressMethodArg.
  4231     theCompressMethod := theCompressMethodArg.
  4211 
  4232 
  4212     ((theCompressMethod == COMPR_DEFLATED) 
  4233     ((theCompressMethod == COMPRESSION_DEFLATED) 
  4213     or:[ theCompressMethod == COMPR_STORED ]) ifFalse:[
  4234     or:[ theCompressMethod == COMPRESSION_STORED ]) ifFalse:[
  4214         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4235         UnsupportedZipFileFormatErrorSignal raiseRequestErrorString:'unsupported compressMethod'.
  4215         "/ if proceeded, write as uncompressed
  4236         "/ if proceeded, write as uncompressed
  4216         theCompressMethod := COMPR_STORED
  4237         theCompressMethod := COMPRESSION_STORED
  4217     ].
  4238     ].
  4218 
  4239 
  4219     zipEntry := ZipMember new default.
  4240     zipEntry := ZipMember new default.
  4220     self addMember:zipEntry.
  4241     self addMember:zipEntry.
  4221 
  4242 
  4240 
  4261 
  4241     zipEntry writeTo:file.
  4262     zipEntry writeTo:file.
  4242 
  4263 
  4243     ^ (ZipWriteStream zipFileStream:file zipEntry:zipEntry)
  4264     ^ (ZipWriteStream zipFileStream:file zipEntry:zipEntry)
  4244         zipArchive:self.
  4265         zipArchive:self.
       
  4266 
       
  4267     "Modified: / 19-11-2010 / 15:38:54 / cg"
  4245 ! !
  4268 ! !
  4246 
  4269 
  4247 !ZipArchive::AbstractZipStream class methodsFor:'instance creation'!
  4270 !ZipArchive::AbstractZipStream class methodsFor:'instance creation'!
  4248 
  4271 
  4249 zipFileStream:something zipEntry:compressionMethodArg
  4272 zipFileStream:something zipEntry:compressionMethodArg
  4746     "represent myself on aStream"
  4769     "represent myself on aStream"
  4747 
  4770 
  4748     relativeLocalHeaderOffset := aStream position.
  4771     relativeLocalHeaderOffset := aStream position.
  4749 
  4772 
  4750     aStream 
  4773     aStream 
  4751         nextPutLong:16r04034b50  MSB:false;
  4774         nextPutLong:C_LOCAL_HEADER_SIGNATURE  MSB:false;
  4752         nextPutShort:versionNeedToExtract MSB:false;
  4775         nextPutShort:versionNeedToExtract MSB:false;
  4753         nextPutShort:generalPurposBitFlag MSB:false;
  4776         nextPutShort:generalPurposBitFlag MSB:false;
  4754         nextPutShort:compressionMethod MSB:false;
  4777         nextPutShort:compressionMethod MSB:false;
  4755         nextPutShort:lastModFileTime MSB:false;
  4778         nextPutShort:lastModFileTime MSB:false;
  4756         nextPutShort:lastModFileDate MSB:false;
  4779         nextPutShort:lastModFileDate MSB:false;
  4760         nextPutShort:fileNameLength MSB:false;
  4783         nextPutShort:fileNameLength MSB:false;
  4761         nextPutShort:extraFieldLength MSB:false;
  4784         nextPutShort:extraFieldLength MSB:false;
  4762         nextPutAll:fileName.
  4785         nextPutAll:fileName.
  4763 
  4786 
  4764     extraField notNil ifTrue: [
  4787     extraField notNil ifTrue: [
       
  4788         self assert:(extraField size = extraFieldLength).
  4765         aStream nextPutAll:extraField.
  4789         aStream nextPutAll:extraField.
  4766     ].
  4790     ].
       
  4791 
       
  4792     "Modified: / 19-11-2010 / 15:45:38 / cg"
  4767 ! !
  4793 ! !
  4768 
  4794 
  4769 !ZipArchive::ZipReadStream methodsFor:'closing'!
  4795 !ZipArchive::ZipReadStream methodsFor:'closing'!
  4770 
  4796 
  4771 close
  4797 close
  4781     startDataPosition := zipFileStream position0Based.
  4807     startDataPosition := zipFileStream position0Based.
  4782     crc32 := 0.
  4808     crc32 := 0.
  4783     uncompressedDataSize := zipEntry uncompressedSize.
  4809     uncompressedDataSize := zipEntry uncompressedSize.
  4784     readPosition := 0.
  4810     readPosition := 0.
  4785 
  4811 
  4786     zipEntry compressionMethod == ZipArchive COMPR_DEFLATED ifTrue:[
  4812     zipEntry compressionMethod == COMPRESSION_DEFLATED ifTrue:[
  4787         compressingStream := ZipStream readOpenAsZipStreamOn:zipFileStream.
  4813         compressingStream := ZipStream readOpenAsZipStreamOn:zipFileStream.
  4788     ] ifFalse:[
  4814     ] ifFalse:[
  4789         compressingStream := zipFileStream.
  4815         compressingStream := zipFileStream.
  4790         compressingStream text.
  4816         compressingStream text.
  4791     ].
  4817     ].
       
  4818 
       
  4819     "Modified: / 19-11-2010 / 15:47:14 / cg"
  4792 ! !
  4820 ! !
  4793 
  4821 
  4794 !ZipArchive::ZipReadStream methodsFor:'queries'!
  4822 !ZipArchive::ZipReadStream methodsFor:'queries'!
  4795 
  4823 
  4796 atEnd
  4824 atEnd
  4888     zipFileStream := something.
  4916     zipFileStream := something.
  4889     startDataPosition := zipFileStream position0Based.
  4917     startDataPosition := zipFileStream position0Based.
  4890     crc32 := 0.
  4918     crc32 := 0.
  4891     uncompressedDataSize := 0.
  4919     uncompressedDataSize := 0.
  4892 
  4920 
  4893     zipEntry compressionMethod == ZipArchive COMPR_DEFLATED ifTrue:[
  4921     zipEntry compressionMethod == COMPRESSION_DEFLATED ifTrue:[
  4894         compressingStream := ZipStream writeOpenAsZipStreamOn:zipFileStream.
  4922         compressingStream := ZipStream writeOpenAsZipStreamOn:zipFileStream.
  4895     ] ifFalse:[
  4923     ] ifFalse:[
  4896         compressingStream := zipFileStream.
  4924         compressingStream := zipFileStream.
  4897     ].
  4925     ].
       
  4926 
       
  4927     "Modified: / 19-11-2010 / 15:46:57 / cg"
  4898 ! !
  4928 ! !
  4899 
  4929 
  4900 !ZipArchive::ZipWriteStream methodsFor:'queries'!
  4930 !ZipArchive::ZipWriteStream methodsFor:'queries'!
  4901 
  4931 
  4902 isReadable
  4932 isReadable
  4967 ! !
  4997 ! !
  4968 
  4998 
  4969 !ZipArchive class methodsFor:'documentation'!
  4999 !ZipArchive class methodsFor:'documentation'!
  4970 
  5000 
  4971 version
  5001 version
  4972     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.85 2010-09-08 11:06:19 stefan Exp $'
  5002     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.86 2010-11-19 15:00:12 cg Exp $'
  4973 !
  5003 !
  4974 
  5004 
  4975 version_CVS
  5005 version_CVS
  4976     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.85 2010-09-08 11:06:19 stefan Exp $'
  5006     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.86 2010-11-19 15:00:12 cg Exp $'
  4977 ! !
  5007 ! !
  4978 
  5008 
  4979 ZipArchive initialize!
  5009 ZipArchive initialize!