ZipArchive.st
changeset 3527 6d61656dca46
parent 3268 71107ddaa66b
child 3528 f64b2fe2bad7
--- a/ZipArchive.st	Mon Mar 09 18:14:52 2015 +0100
+++ b/ZipArchive.st	Mon Mar 09 18:55:09 2015 +0100
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 1998 by eXept Software AG
               All Rights Reserved
@@ -11,6 +13,8 @@
 "
 "{ Package: 'stx:libbasic2' }"
 
+"{ NameSpace: Smalltalk }"
+
 Object subclass:#ZipArchive
 	instanceVariableNames:'file mode archiveName firstEntry lastEntry centralDirectory
 		startOfArchive endOfArchive zipMembersByName appendTrailingSlash'
@@ -3548,12 +3552,25 @@
 !ZipArchive methodsFor:'private - decompression'!
 
 decode:rawBytes method:compressionMethod size:uncompressedSize
+    "decode rawBytes into a byteArray"
+
+    ^ self
+        decode:rawBytes 
+        method:compressionMethod 
+        size:uncompressedSize
+        asString:false
+!
+
+decode:rawBytes method:compressionMethod size:uncompressedSize asString:asString
+    "decode rawBytes into a byteArray or string"
+
     |outBytes|
 
     compressionMethod == COMPRESSION_STORED ifTrue:[
         "/
         "/ uncompressed
         "/
+        asString ifTrue:[^ rawBytes asString].
         ^ rawBytes
     ].
 
@@ -3561,7 +3578,11 @@
         "/
         "/ deflate/inflate algorithm
         "/
-        outBytes := ByteArray new:uncompressedSize.
+        asString ifTrue:[
+            outBytes := String new:uncompressedSize.
+        ] ifFalse:[
+            outBytes := ByteArray new:uncompressedSize.
+        ].
         ^ self inflate:rawBytes to:outBytes
     ].
 
@@ -3608,21 +3629,41 @@
     |inflateReturnCode|
 
 %{  /* STACK:32768 */
-    if (__isByteArray(inBytes)
-     && __isByteArray(outBytes)) {
-        char *in, *out;
+    char *in, *out;
+
+    if (__isByteArray(inBytes)) {
+        in = __ByteArrayInstPtr(inBytes)->ba_element;
+    } else if (__isString(inBytes)) {
+        in = __stringVal(inBytes);
+    } else {
+        inflateReturnCode = __MKSMALLINT(-999);
+        goto badArgument;
+    }
+
+    if (__isByteArray(outBytes)) {
+        out = __ByteArrayInstPtr(outBytes)->ba_element;
+    } else if (__isString(outBytes)) {
+        out = __stringVal(outBytes);
+    } else {
+        inflateReturnCode = __MKSMALLINT(-999);
+        goto badArgument;
+    }
+
+    {
         int rc;
 
-        in = __ByteArrayInstPtr(inBytes)->ba_element;
-        out = __ByteArrayInstPtr(outBytes)->ba_element;
-
         if ((rc = stx_inflate(in, out)) == 0) {
             RETURN (outBytes);
         }
         inflateReturnCode = __MKSMALLINT(rc);
     }
+badArgument: ;
 %}.
     inflateReturnCode notNil ifTrue:[
+        inflateReturnCode == -999 ifTrue:[
+            self primitiveFailed:'bad argument'
+        ].
+
         "/ bad blockType 2
         self error:'inflate error: ' , inflateReturnCode printString
     ].
@@ -3959,6 +4000,13 @@
     "extract an entry identified by fileName as a byteArray;
      nil on errors"
 
+    ^ self extract:fileName asString:false
+!
+
+extract:fileName asString:asString
+    "extract an entry identified by fileName as a byteArray or string;
+     nil on errors"
+
     self 
         withPositionAndMemberFor:fileName 
         do:[:zmemb :position |
@@ -3970,7 +4018,8 @@
             data := self
                 decode:rawContents
                 method:(zmemb compressionMethod)
-                size:(zmemb uncompressedSize).
+                size:(zmemb uncompressedSize)
+                asString:asString.
 
             ^ data.
         ].
@@ -5193,11 +5242,11 @@
 !ZipArchive class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.113 2014-04-29 13:27:27 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.114 2015-03-09 17:55:09 cg Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.113 2014-04-29 13:27:27 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipArchive.st,v 1.114 2015-03-09 17:55:09 cg Exp $'
 ! !