如何压缩Squeak Smalltalk中的目录?

时间:2013-08-30 21:31:18

标签: compression zip smalltalk squeak

如何在Squeak Smalltalk中压缩目录?我在StandardFileStream中找到了compressFile方法,但我无法弄清楚如何压缩多个文件或目录。我试验过System-Compression类,但运气不好。提前谢谢!

这就是我现在所拥有的。我在文件名末尾添加时间戳,所以在这里我想把所有以给定文件名开头的文件放入zip或gzip文件中。

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.

1 个答案:

答案 0 :(得分:6)

ZipWriteStream只是一个用于压缩的辅助类,它不知道如何布置正确的ZIP文件,包含所有标头和目录信息等。您想使用ZipArchive

"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.