support for lazy loading;
authorClaus Gittinger <cg@exept.de>
Sun, 21 Nov 2010 12:06:39 +0100
changeset 2514 de7b7070061c
parent 2513 27c989d8f1d5
child 2515 104080163067
support for lazy loading; code cleanup
ZipArchive.st
--- a/ZipArchive.st	Fri Nov 19 17:47:48 2010 +0100
+++ b/ZipArchive.st	Sun Nov 21 12:06:39 2010 +0100
@@ -3236,12 +3236,24 @@
     aPositionableStream isFileStream ifTrue:[
         archiveName := aPositionableStream pathName.
         aPositionableStream isDirectory ifTrue:[
-            OpenError raiseWith:self errorString:' - is a directory'.
+            OpenError raiseWith:self errorString:(' - is a directory').
         ].        
     ] ifFalse:[
         archiveName := 'internal stream'.
     ].
     self readDirectory.
+
+    "Modified: / 21-11-2010 / 11:45:53 / cg"
+!
+
+reopenForReading
+    file isNil ifTrue:[
+        mode := #read.
+        file := archiveName asFilename readStream.
+        file binary
+    ]
+
+    "Created: / 21-11-2010 / 12:02:37 / cg"
 !
 
 writingTo:aPositionableStream
@@ -3750,100 +3762,88 @@
     "extract an entry identified by fileName as a byteArray;
      nil on errors"
 
-    |zmemb rawContents data|
-
-    (file isNil or:[mode ~~ #read]) ifTrue:[
-        ^ self error: 'ZipArchive not open for reading ...'.
-    ].    
-
-    zmemb := self findMember:fileName.
-    zmemb isNil ifTrue:[^ nil].
-    (zmemb fileStart + startOfArchive) > endOfArchive ifTrue: [
-        ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry start is out of the archive bounds'.
-    ].
-
-    (zmemb fileStart + startOfArchive + (zmemb compressedSize)) > endOfArchive ifTrue: [
-        ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry end is out of the archive bounds'.
-    ].
-
-    file position0Based:(zmemb fileStart + startOfArchive).
-    rawContents := file nextBytes:(zmemb compressedSize).
-
-    data := self
+    self 
+        withPositionAndMemberFor:fileName 
+        do:[:zmemb :position |
+            |rawContents data|
+
+            file position0Based:position.
+            rawContents := file nextBytes:(zmemb compressedSize).
+
+            data := self
                 decode:rawContents
                 method:(zmemb compressionMethod)
                 size:(zmemb uncompressedSize).
 
-    ^ data.
+            ^ data.
+        ].
+
+    "Modified: / 21-11-2010 / 11:53:00 / cg"
 !
 
 extract:fileName intoStream: aWriteStream
-    "extract an entry indentified by filename into aWriteStream
-     return false on error"
-
-    |zmemb buffer rdSize nextBlockSize streamBufferSize myZipStream|
-
-    (file isNil or:[mode ~~ #read]) ifTrue:[
-        self error:'ZipArchive not open for reading ...'.
-        ^ false
-    ].    
-
-    zmemb := self findMember:fileName.
-    zmemb isNil ifTrue:[^ false].
-
-    (zmemb fileStart + startOfArchive) > endOfArchive ifTrue: [
-        ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry start is out of the archive bounds'.
-        ^ false
-    ].
-
-    (zmemb fileStart + startOfArchive + (zmemb compressedSize)) > endOfArchive ifTrue: [
-        ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry end is out of the archive bounds'.
-        ^ false
-    ].
-
-    file position0Based:(zmemb fileStart + startOfArchive).
-
-    rdSize := zmemb uncompressedSize.
-    streamBufferSize := self class streamBufferSize.    
-    buffer := ByteArray new: streamBufferSize.
-    [
-        [rdSize > 0] whileTrue: [
-            rdSize > (self class streamBufferSize) ifTrue: [
-                nextBlockSize := streamBufferSize.
-            ] ifFalse: [
-                (nextBlockSize := rdSize) > 0 ifTrue: [
-                    buffer := ByteArray new: nextBlockSize.
+    "extract an entry indentified by filename into aWriteStream"
+
+    self 
+        withPositionAndMemberFor:fileName 
+        do:[:zmemb :position |
+            |buffer rdSize nextBlockSize streamBufferSize myZipStream|
+
+            file position0Based:position.
+
+            rdSize := zmemb uncompressedSize.
+            streamBufferSize := self class streamBufferSize.    
+            buffer := ByteArray new: streamBufferSize.
+            [
+                [rdSize > 0] whileTrue: [
+                    rdSize > (self class streamBufferSize) ifTrue: [
+                        nextBlockSize := streamBufferSize.
+                    ] ifFalse: [
+                        (nextBlockSize := rdSize) > 0 ifTrue: [
+                            buffer := ByteArray new: nextBlockSize.
+                        ].
+                    ].
+
+                    nextBlockSize > 0 ifTrue: [
+                        zmemb compressionMethod == COMPRESSION_DEFLATED ifTrue:[
+                            myZipStream isNil ifTrue: [
+                                file binary.
+                                myZipStream := ZipStream readOpenAsZipStreamOn: file.
+                            ].
+                            buffer := myZipStream next:nextBlockSize.
+                        ] ifFalse:[
+                            zmemb compressionMethod == COMPRESSION_STORED ifTrue:[
+                                file nextBytes:nextBlockSize into:buffer startingAt:1.
+                            ] ifFalse:[
+                                UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
+                            ].
+                        ].
+
+                        aWriteStream nextPutBytes:buffer size from:buffer startingAt:1.
+                    ].
+                    rdSize := rdSize - nextBlockSize.
+                ].
+            ] ensure:[
+                myZipStream notNil ifTrue:[
+                    myZipStream close.
                 ].
             ].
-
-            nextBlockSize > 0 ifTrue: [
-                zmemb compressionMethod == COMPRESSION_DEFLATED ifTrue:[
-                    myZipStream isNil ifTrue: [
-                        file binary.
-                        myZipStream := ZipStream readOpenAsZipStreamOn: file.
-                    ].
-                    buffer := myZipStream next:nextBlockSize.
-                ] ifFalse:[
-                    zmemb compressionMethod == COMPRESSION_STORED ifTrue:[
-                        file nextBytes:nextBlockSize into:buffer startingAt:1.
-                    ] ifFalse:[
-                        UnsupportedZipFileFormatErrorSignal raiseErrorString:'unsupported compressMethod'
-                    ].
-                ].
-
-                aWriteStream nextPutBytes:buffer size from:buffer startingAt:1.
-            ].
-            rdSize := rdSize - nextBlockSize.
-        ].
-    ] ensure:[
-        myZipStream notNil ifTrue:[
-            myZipStream close.
-        ].
+        ]
+
+    "Modified: / 21-11-2010 / 11:56:51 / cg"
+!
+
+reopenAndExtract:fileName intoStream: aWriteStream
+    "extract an entry indentified by filename into aWriteStream"
+
+    file isNil ifTrue:[
+        self reopenForReading.
     ].
-
-    ^ true.
-
-    "Modified: / 19-11-2010 / 15:58:24 / cg"
+    self extract:fileName intoStream: aWriteStream.
+    file close.
+    file := nil.
+
+    "Created: / 21-11-2010 / 11:59:04 / cg"
 !
 
 restoreOsDirectory:osDirectoryName fromArchiveDirectory: archiveDirectoryName
@@ -3901,6 +3901,28 @@
             ].
         ]
     ].
+!
+
+withPositionAndMemberFor:fileName do:aBlock
+    |zmemb  |
+
+    (file isNil or:[mode ~~ #read]) ifTrue:[
+        ^ self error: 'ZipArchive not open for reading ...'.
+    ].    
+
+    zmemb := self findMember:fileName.
+    zmemb isNil ifTrue:[^ nil].
+    (zmemb fileStart + startOfArchive) > endOfArchive ifTrue: [
+        ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry start is out of the archive bounds'.
+    ].
+
+    (zmemb fileStart + startOfArchive + (zmemb compressedSize)) > endOfArchive ifTrue: [
+        ^ ZipFileFormatErrorSignal raiseRequestErrorString:' - zipEntry end is out of the archive bounds'.
+    ].
+
+    aBlock value:zmemb value:(zmemb fileStart + startOfArchive)
+
+    "Created: / 21-11-2010 / 11:51:41 / cg"
 ! !
 
 !ZipArchive methodsFor:'reading - stream'!
@@ -4997,11 +5019,11 @@
 !ZipArchive class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.88 2010-11-19 16:47:48 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.89 2010-11-21 11:06:39 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.88 2010-11-19 16:47:48 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.89 2010-11-21 11:06:39 cg Exp $'
 ! !
 
 ZipArchive initialize!