如何压缩Squeak Smalltalk中的目录? [英] How do you compress a directory in Squeak Smalltalk?

查看:247
本文介绍了如何压缩Squeak Smalltalk中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何压缩Squeak Smalltalk中的目录?我发现在StandardFileStream的compressFile方法,但我不知道如何压缩多个文件或一个目录。我试验了System-Compression类,但没有太多的运气。先感谢!

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

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

This is what I have now. I'm adding time stamps at the end of file names, so here I want to put all files that begin with the given file name into 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.


推荐答案

ZipWriteStream 只是一个用于压缩的帮助类,它不知道如何正确的ZIP文件被布局,所有的头和目录信息等。你想使用 ZipArchive class。

ZipWriteStream is only a helper class used for compressing, it does not know how a proper ZIP file is layed out, 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.

这篇关于如何压缩Squeak Smalltalk中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆