compression - 如何在 Squeak Smalltalk 中压缩目录?

标签 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.

最佳答案

ZipWriteStream只是一个用于压缩的辅助类,它不知道一个合适的 ZIP 文件是如何布局的,包括所有的头和目录信息等。你想使用 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.

关于compression - 如何在 Squeak Smalltalk 中压缩目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18541751/

相关文章:

德尔福/TZipFile : how to restore the file's original timestamp?

python - 如何在 Julia 中使用 zip(*list) 解包机制进行迭代?

jquery - 如何在 Smalltalk 中使用 jQuery 禁用 html div 元素(输入字段、文本区域等)?

c++ - C++ 的 gzstream 库 : corrupted file created

Java压缩集合库

c# - 提取 zip 文件并覆盖(在同一目录中 - C#)

smalltalk - 如何将对象的类型存储在变量中?

class - 测试一个类的实例

android - 如何减少移动使用的 mp3 流媒体流量消耗?

http - 低流量网站上的 HTTP 压缩(GZip 或 deflate)真的有用吗?