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