How do you compress a directory in Squeak Smalltalk?

How do you compress a directory in Squeak Smalltalk? I found the compressFile method in StandardFileStream, but I cannot figure out how to compress multiple files or a directory. I experimented with System-Compression classes, but didn't have much luck. Thanks in advance!

This is what I have now. I am adding timestamps at the end of the file names, so here I want to put all the files starting with the specified file name in a zip or gzip file.

compressFile: aFileName in: aDirectory | zipped buffer unzipped zipFileName | zipFileName _ aFileName copyUpTo: $. . zipped _ aDirectory newFileNamed: (zipFileName, FileDirectory dot, 'zip'). zipped binary; setFileTypeToObject. zipped _ ZipWriteStream on: zipped. buffer _ ByteArray new: 50000. aDirectory fileNames do: [:f | (f beginsWith: zipFileName) ifTrue: [ unzipped _ aDirectory readOnlyFileNamed: (aDirectory fullNameFor: f). unzipped binary. [unzipped atEnd] whileFalse:[ zipped nextPutAll: (unzipped nextInto: buffer)]. unzipped close]]. zipped close. 
+4
source share
1 answer

ZipWriteStream is just a helper class used for compression, it does not know how to create the desired ZIP file, with all the header and directory information, etc. You want to use the ZipArchive class.

 "first, construct archive layout in memory" zip := ZipArchive new. zip addFile: 'foo.txt'. zip addFile: 'bar.txt' as: 'xyz.txt'. zip addTree: dir match: [:entry | entry name beginswith: 'baz']. "then, write archive to disk, compressing each member" file := dest newFileNamed: 'test.zip'. zip writeTo: file. file close. 
+6
source

Source: https://habr.com/ru/post/1499875/


All Articles