如何在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.
答案 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.