class: ZipStream
authorStefan Vogel <sv@exept.de>
Mon, 19 May 2014 16:41:09 +0200
changeset 3283 836136cf9e8d
parent 3282 66ed7ab6329d
child 3284 0fb781a3260d
class: ZipStream comment/format in: #examples #initialize #readHeader changed: #writeHeader
ZipStream.st
--- a/ZipStream.st	Mon May 19 16:40:40 2014 +0200
+++ b/ZipStream.st	Mon May 19 16:41:09 2014 +0200
@@ -102,7 +102,7 @@
 
         stream := '' writeStream.
 
-        [ (c := zipStream next) notNil ] whileTrue:[
+        [ (c := zipStream nextOrNil) notNil ] whileTrue:[
             stream nextPut:c
         ].
         stream close.
@@ -113,12 +113,9 @@
 
 
                                                                 [exBegin]
-    |file fstream zipStream c|
+    |fstream zipStream c|
 
-    file := 'C:\gg.zip' asFilename.
-    file exists ifTrue:[ file delete ].
-
-    fstream := file writeStream.
+    fstream := FileStream newTemporary.
     zipStream := ZipStream writeOpenOn:fstream.
     zipStream nextPutAll:'hallo Welt(1) - test....'.
     zipStream cr.
@@ -128,15 +125,14 @@
     zipStream close. 
     fstream close.
 
-    fstream := file readStream.
+    fstream := fstream fileName readStream.
     zipStream := ZipStream readOpenOn:fstream.
 
-    [ (c := zipStream next) notNil ] whileTrue:[
-        Transcript show:c.
-    ].
-    Transcript cr.
+    Transcript showCR:zipStream contents.
+
     zipStream close.
     fstream close.
+    fstream fileName delete.
                                                                 [exEnd]
 
 "
@@ -145,8 +141,8 @@
 !ZipStream class methodsFor:'initialization'!
 
 initialize
-    "setup class attributes derived from the library
-    "
+    "setup class attributes derived from the library"
+
     |z_deflated os_code|
 %{
     z_deflated = __MKSMALLINT( Z_DEFLATED );
@@ -163,7 +159,6 @@
     HEAD_CRC              := 16r02.     " bit 1 set:  header CRC present "
 
     GZ_MAGIC_ID           := #[ 16r1f 16r8b ]
-
 ! !
 
 !ZipStream class methodsFor:'instance creation'!
@@ -809,54 +804,52 @@
     |flags|
 
     GZ_MAGIC_ID do:[:b|
-	onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
+        onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
     ].
 
     onStream nextByte ~~ Z_DEFLATED ifTrue:[
-	self zerror:'invalid method (not deflated)'
+        self zerror:'invalid method (not deflated)'
     ].
 
     flags := onStream nextByte.
     (flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
-	self zerror:'wrong data format'
+        self zerror:'wrong data format'
     ].
 
     "discard time, xflags and OS code"
     onStream skip:6.
 
     (flags bitAnd:HEAD_EXTRA_FIELD) ~~ 0 ifTrue:[|len|
-	"skip the extra field"
-	len := onStream nextByte + (onStream nextByte bitShift:8).
-	len timesRepeat:[ onStream nextByte ].
+        "skip the extra field"
+        len := onStream nextByte + (onStream nextByte bitShift:8).
+        len timesRepeat:[ onStream nextByte ].
     ].
 
-    (flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[|b|
-	"skip the original file name"
-	[ (b := onStream nextByte) ~~ 0 ] whileTrue.
+    (flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[
+        |b|
+        "skip the original file name"
+        [ (b := onStream nextByte) ~~ 0 ] whileTrue.
     ].
 
     (flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
-	"skip the header crc"
-	onStream skip:2.
+        "skip the header crc"
+        onStream skip:2.
     ].
 !
 
 writeHeader
-    "write the gzip magic id"
 
-    GZ_MAGIC_ID do:[:b| onStream nextPutByte:b ].
+    "write the gzip magic id"      
+    onStream nextPutBytes:GZ_MAGIC_ID.
 
     "write the method"
     onStream nextPutByte:Z_DEFLATED.
 
-    "write the flags"
-    onStream nextPutByte:0.
-
-    "write time"
-    4 timesRepeat:[ onStream nextPutByte:0 ].
-
-    "write xflags"
-    onStream nextPutByte:0.
+    onStream nextPutBytes:#[
+                0                    "write the flags"  
+                0 0 0 0              "write time"
+                0                    "write xflags"
+            ].
 
     "write OS code"
     onStream nextPutByte:HEAD_OS_CODE.
@@ -865,11 +858,12 @@
 !ZipStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.40 2011-09-11 15:11:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.41 2014-05-19 14:41:09 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.40 2011-09-11 15:11:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.41 2014-05-19 14:41:09 stefan Exp $'
 ! !
 
+
 ZipStream initialize!