ZipArchive.st
changeset 3529 e074058c7ee5
parent 3528 f64b2fe2bad7
child 3643 ab2b809aeb5b
child 3665 3e5128566dc2
equal deleted inserted replaced
3528:f64b2fe2bad7 3529:e074058c7ee5
  3148 ! !
  3148 ! !
  3149 
  3149 
  3150 !ZipArchive methodsFor:'Compatibility-Squeak'!
  3150 !ZipArchive methodsFor:'Compatibility-Squeak'!
  3151 
  3151 
  3152 binaryContentsOf: fileName
  3152 binaryContentsOf: fileName
  3153 
  3153     ^ self extract: fileName asString:false.
  3154     ^(self extract: fileName asString:false) 
       
  3155 !
  3154 !
  3156 
  3155 
  3157 desiredCompressionMethod:aCompressionMethod
  3156 desiredCompressionMethod:aCompressionMethod
  3158     "for now: ignored"
  3157     "for now: ignored"
  3159 ! !
  3158 ! !
  3370 
  3369 
  3371     "Created: / 21-11-2010 / 12:02:37 / cg"
  3370     "Created: / 21-11-2010 / 12:02:37 / cg"
  3372 !
  3371 !
  3373 
  3372 
  3374 writingTo:aPositionableStream
  3373 writingTo:aPositionableStream
  3375     "initialize the archive to read from aPositionableStream"
  3374     "initialize the archive to write to aPositionableStream"
  3376 
  3375 
  3377     file notNil ifTrue: [
  3376     file notNil ifTrue: [
  3378         self closeFile.
  3377         self closeFile.
  3379     ].
  3378     ].
  3380 
  3379 
  3388     ].
  3387     ].
  3389     zipMembersByName := Dictionary new.
  3388     zipMembersByName := Dictionary new.
  3390 ! !
  3389 ! !
  3391 
  3390 
  3392 !ZipArchive methodsFor:'private'!
  3391 !ZipArchive methodsFor:'private'!
  3393 
       
  3394 checkZipArchive:archiveFileName
       
  3395     |isValidArchive|
       
  3396 
       
  3397     archiveName := archiveFileName asFilename name.
       
  3398     isValidArchive := false.
       
  3399     mode := #read.
       
  3400     self openFile.
       
  3401 
       
  3402     [
       
  3403         isValidArchive := self checkZipArchive.
       
  3404     ] ensure:[
       
  3405         self closeFile.
       
  3406     ].
       
  3407     ^ isValidArchive
       
  3408 !
       
  3409 
  3392 
  3410 closeFile
  3393 closeFile
  3411     "finish the zip archive and close the stream"
  3394     "finish the zip archive and close the stream"
  3412 
  3395 
  3413     |savedFile|
  3396     |savedFile|
  3629     |inflateReturnCode|
  3612     |inflateReturnCode|
  3630 
  3613 
  3631 %{  /* STACK:32768 */
  3614 %{  /* STACK:32768 */
  3632     char *in, *out;
  3615     char *in, *out;
  3633 
  3616 
  3634     if (__isByteArray(inBytes)) {
  3617     if (__isByteArrayLike(inBytes)) {
  3635         in = __ByteArrayInstPtr(inBytes)->ba_element;
  3618         in = __byteArrayVal(inBytes);
  3636     } else if (__isString(inBytes)) {
  3619     } else if (__isStringLike(inBytes)) {
  3637         in = __stringVal(inBytes);
  3620         in = __stringVal(inBytes);
  3638     } else {
  3621     } else {
  3639         inflateReturnCode = __MKSMALLINT(-999);
  3622         inflateReturnCode = @symbol(badArgument1);
  3640         goto badArgument;
  3623         goto badArgument;
  3641     }
  3624     }
  3642 
  3625 
  3643     if (__isByteArray(outBytes)) {
  3626     if (__isByteArray(outBytes)) {
  3644         out = __ByteArrayInstPtr(outBytes)->ba_element;
  3627         out = __byteArrayVal(outBytes);
  3645     } else if (__isString(outBytes)) {
  3628     } else if (__isString(outBytes)) {
  3646         out = __stringVal(outBytes);
  3629         out = __stringVal(outBytes);
  3647     } else {
  3630     } else {
  3648         inflateReturnCode = __MKSMALLINT(-999);
  3631         inflateReturnCode = @symbol(badArgument2);
  3649         goto badArgument;
  3632         goto badArgument;
  3650     }
  3633     }
  3651 
  3634 
  3652     {
  3635     {
  3653         int rc;
  3636         int rc = stx_inflate(in, out);
  3654 
  3637 
  3655         if ((rc = stx_inflate(in, out)) == 0) {
  3638         if (rc == 0) {
  3656             RETURN (outBytes);
  3639             RETURN (outBytes);
  3657         }
  3640         }
  3658         inflateReturnCode = __MKSMALLINT(rc);
  3641         inflateReturnCode = __MKSMALLINT(rc);
  3659     }
  3642     }
  3660 badArgument: ;
  3643 badArgument: ;
  3661 %}.
  3644 %}.
  3662     inflateReturnCode notNil ifTrue:[
  3645     inflateReturnCode notNil ifTrue:[
  3663         inflateReturnCode == -999 ifTrue:[
  3646         inflateReturnCode isSymbol ifTrue:[
  3664             self primitiveFailed:'bad argument'
  3647             self primitiveFailed:inflateReturnCode
  3665         ].
  3648         ].
  3666 
  3649 
  3667         "/ bad blockType 2
  3650         "/ bad blockType 2
  3668         self error:'inflate error: ' , inflateReturnCode printString
  3651         self error:'inflate error: ' , inflateReturnCode printString
  3669     ].
  3652     ].
  3766     "Modified: / 30.3.1998 / 17:13:20 / cg"
  3749     "Modified: / 30.3.1998 / 17:13:20 / cg"
  3767     "Created: / 9.9.1998 / 20:33:06 / cg"
  3750     "Created: / 9.9.1998 / 20:33:06 / cg"
  3768 !
  3751 !
  3769 
  3752 
  3770 checkZipArchive
  3753 checkZipArchive
  3771     "read the zip directory into a linked-list of zipMembers"
  3754     "check if my file is really a zip archive. answer true or false."
  3772 
  3755 
  3773     |size count_in|
  3756     |size count_in|
  3774 
  3757 
  3775     file isNil ifTrue: [
  3758     file isNil ifTrue: [
  3776         ^ false
  3759         ^ false
  3787     (size < (ECREC_SIZE+4)) ifTrue:[
  3770     (size < (ECREC_SIZE+4)) ifTrue:[
  3788         ^ false.
  3771         ^ false.
  3789     ].
  3772     ].
  3790 
  3773 
  3791     ^ self searchForEndOfCentralDirectorySignature
  3774     ^ self searchForEndOfCentralDirectorySignature
       
  3775 !
       
  3776 
       
  3777 checkZipArchive:archiveFileName
       
  3778     |isValidArchive|
       
  3779 
       
  3780     archiveName := archiveFileName asFilename name.
       
  3781     isValidArchive := false.
       
  3782     mode := #read.
       
  3783     self openFile.
       
  3784 
       
  3785     [
       
  3786         isValidArchive := self checkZipArchive.
       
  3787     ] ensure:[
       
  3788         self closeFile.
       
  3789     ].
       
  3790     ^ isValidArchive
  3792 !
  3791 !
  3793 
  3792 
  3794 findMember:name
  3793 findMember:name
  3795     "find a zipMember by name"
  3794     "find a zipMember by name"
  3796 
  3795 
  4026 
  4025 
  4027     ^ nil
  4026     ^ nil
  4028 
  4027 
  4029     "Modified: / 21-11-2010 / 11:53:00 / cg"
  4028     "Modified: / 21-11-2010 / 11:53:00 / cg"
  4030     "Modified: / 22-12-2010 / 12:24:54 / sr"
  4029     "Modified: / 22-12-2010 / 12:24:54 / sr"
  4031 !
       
  4032 
       
  4033 extract:fileName intoStream: aWriteStream
       
  4034     "extract an entry indentified by filename into aWriteStream"
       
  4035 
       
  4036     self 
       
  4037         withPositionAndMemberFor:fileName 
       
  4038         do:[:zmemb :position |
       
  4039             |buffer rdSize nextBlockSize streamBufferSize myZipStream|
       
  4040 
       
  4041             file position:position.
       
  4042 
       
  4043             rdSize := zmemb uncompressedSize.
       
  4044             streamBufferSize := self class streamBufferSize.    
       
  4045             buffer := ByteArray new: streamBufferSize.
       
  4046             [
       
  4047                 [rdSize > 0] whileTrue: [
       
  4048                     rdSize > (self class streamBufferSize) ifTrue: [
       
  4049                         nextBlockSize := streamBufferSize.
       
  4050                     ] ifFalse: [
       
  4051                         (nextBlockSize := rdSize) > 0 ifTrue: [
       
  4052                             buffer := ByteArray new: nextBlockSize.
       
  4053                         ].
       
  4054                     ].
       
  4055 
       
  4056                     nextBlockSize > 0 ifTrue: [
       
  4057                         zmemb compressionMethod == COMPRESSION_DEFLATED ifTrue:[
       
  4058                             myZipStream isNil ifTrue: [
       
  4059                                 file binary.
       
  4060                                 myZipStream := ZipStream readOpenAsZipStreamOn: file.
       
  4061                             ].
       
  4062                             buffer := myZipStream next:nextBlockSize.
       
  4063                         ] ifFalse:[
       
  4064                             zmemb compressionMethod == COMPRESSION_STORED ifTrue:[
       
  4065                                 file nextBytes:nextBlockSize into:buffer startingAt:1.
       
  4066                             ] ifFalse:[
       
  4067                                 UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
       
  4068                             ].
       
  4069                         ].
       
  4070 
       
  4071                         aWriteStream nextPutBytes:buffer size from:buffer startingAt:1.
       
  4072                     ].
       
  4073                     rdSize := rdSize - nextBlockSize.
       
  4074                 ].
       
  4075             ] ensure:[
       
  4076                 myZipStream notNil ifTrue:[
       
  4077                     myZipStream close.
       
  4078                 ].
       
  4079             ].
       
  4080         ]
       
  4081 
       
  4082     "Modified: / 21-11-2010 / 11:56:51 / cg"
       
  4083 !
       
  4084 
       
  4085 reopenAndExtract:fileName intoStream: aWriteStream
       
  4086     "extract an entry indentified by filename into aWriteStream"
       
  4087 
       
  4088     file isNil ifTrue:[
       
  4089         self reopenForReading.
       
  4090     ].
       
  4091     self extract:fileName intoStream: aWriteStream.
       
  4092     file close.
       
  4093     file := nil.
       
  4094 
       
  4095     "Created: / 21-11-2010 / 11:59:04 / cg"
       
  4096 !
  4030 !
  4097 
  4031 
  4098 restoreOsDirectory:osDirectoryName fromArchiveDirectory: archiveDirectoryName
  4032 restoreOsDirectory:osDirectoryName fromArchiveDirectory: archiveDirectoryName
  4099     |osDirectory directoryAlreadyCreated archiveDirectoryNameSize|
  4033     |osDirectory directoryAlreadyCreated archiveDirectoryNameSize|
  4100 
  4034 
  4169 ! !
  4103 ! !
  4170 
  4104 
  4171 
  4105 
  4172 !ZipArchive methodsFor:'reading - stream'!
  4106 !ZipArchive methodsFor:'reading - stream'!
  4173 
  4107 
       
  4108 extract:fileName intoStream: aWriteStream
       
  4109     "extract an entry indentified by filename into aWriteStream"
       
  4110 
       
  4111     self 
       
  4112         withPositionAndMemberFor:fileName 
       
  4113         do:[:zmemb :position |
       
  4114             |buffer rdSize compressionMethod nextBlockSize streamBufferSize myZipStream|
       
  4115 
       
  4116             file position:position.
       
  4117 
       
  4118             compressionMethod := zmemb compressionMethod.
       
  4119             rdSize := zmemb uncompressedSize.
       
  4120             nextBlockSize := streamBufferSize := self class streamBufferSize.    
       
  4121             buffer := ByteArray new: streamBufferSize.
       
  4122             [
       
  4123                 [rdSize > 0] whileTrue: [
       
  4124                     rdSize < streamBufferSize ifTrue: [
       
  4125                         nextBlockSize := rdSize.
       
  4126                     ].
       
  4127 
       
  4128                     compressionMethod == COMPRESSION_DEFLATED ifTrue:[
       
  4129                         myZipStream isNil ifTrue: [
       
  4130                             file binary.
       
  4131                             myZipStream := ZipStream readOpenAsZipStreamOn: file.
       
  4132                         ].
       
  4133                         myZipStream next:nextBlockSize into:buffer startingAt:1.
       
  4134                     ] ifFalse:[compressionMethod == COMPRESSION_STORED ifTrue:[
       
  4135                         file nextBytes:nextBlockSize into:buffer startingAt:1.
       
  4136                     ] ifFalse:[
       
  4137                         UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
       
  4138                     ]].
       
  4139 
       
  4140                     aWriteStream nextPutBytes:nextBlockSize from:buffer startingAt:1.
       
  4141                     rdSize := rdSize - nextBlockSize.
       
  4142                 ].
       
  4143             ] ensure:[
       
  4144                 myZipStream notNil ifTrue:[
       
  4145                     myZipStream close.
       
  4146                 ].
       
  4147             ].
       
  4148         ]
       
  4149 
       
  4150     "Modified: / 21-11-2010 / 11:56:51 / cg"
       
  4151 !
       
  4152 
  4174 readStreamFor:nameOfFileInArchive
  4153 readStreamFor:nameOfFileInArchive
  4175     "open a stream on archive contents identified by nameOfFileInArchive"
  4154     "open a stream on archive contents identified by nameOfFileInArchive"
  4176 
  4155 
  4177     |zipEntry dataStart|
  4156     |zipEntry dataStart|
  4178 
  4157 
  4188     dataStart := self dataStartOf:zipEntry.
  4167     dataStart := self dataStartOf:zipEntry.
  4189     file position:dataStart.
  4168     file position:dataStart.
  4190 
  4169 
  4191     ^ (ZipReadStream zipFileStream:file zipEntry:zipEntry)
  4170     ^ (ZipReadStream zipFileStream:file zipEntry:zipEntry)
  4192         zipArchive:self.
  4171         zipArchive:self.
       
  4172 !
       
  4173 
       
  4174 reopenAndExtract:fileName intoStream: aWriteStream
       
  4175     "extract an entry indentified by filename into aWriteStream"
       
  4176 
       
  4177     file isNil ifTrue:[
       
  4178         self reopenForReading.
       
  4179     ].
       
  4180     self extract:fileName intoStream: aWriteStream.
       
  4181     file close.
       
  4182     file := nil.
       
  4183 
       
  4184     "Created: / 21-11-2010 / 11:59:04 / cg"
  4193 ! !
  4185 ! !
  4194 
  4186 
  4195 !ZipArchive methodsFor:'writing'!
  4187 !ZipArchive methodsFor:'writing'!
  4196 
  4188 
  4197 addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName
  4189 addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName
  5240 ! !
  5232 ! !
  5241 
  5233 
  5242 !ZipArchive class methodsFor:'documentation'!
  5234 !ZipArchive class methodsFor:'documentation'!
  5243 
  5235 
  5244 version
  5236 version
  5245     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.115 2015-03-09 17:59:21 cg Exp $'
  5237     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.116 2015-03-11 22:40:27 stefan Exp $'
  5246 !
  5238 !
  5247 
  5239 
  5248 version_CVS
  5240 version_CVS
  5249     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.115 2015-03-09 17:59:21 cg Exp $'
  5241     ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.116 2015-03-11 22:40:27 stefan Exp $'
  5250 ! !
  5242 ! !
  5251 
  5243 
  5252 
  5244 
  5253 ZipArchive initialize!
  5245 ZipArchive initialize!