2013-08-30 45 views
4

如何壓縮Squeak Smalltalk中的目錄?我在StandardFileStream中找到了compressFile方法,但我無法弄清楚如何壓縮多個文件或目錄。我嘗試了系統壓縮類,但沒有多少運氣。提前致謝!如何在Squeak Smalltalk中壓縮目錄?

這就是我現在擁有的。我在文件名末尾添加了時間戳,所以在這裏我想將以給定文件名開頭的所有文件放入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

請問您是否可以發佈您嘗試過的代碼段並且無法正常工作。 –

+0

我剛剛加了我現在的東西。它運行,但是當我嘗試在終端中解壓縮時,出現此錯誤:未找到中央目錄簽名末尾。這個文件不是 一個zip文件,或者它構成一個多部分檔案的一個磁盤。在 後一種情況下,中心目錄和zipfile註釋將在 上找到此歸檔的最後一個磁盤。 unzip:在test1.zip或 test1.zip.zip之一中找不到zipfile目錄,並且無法找到test1.zip.ZIP,句點。 –

回答

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. 
+0

感謝您的幫助,但我認爲解決方案適用於Pharo。我無法在Squeak中找到ZipArchive類。 –

+1

@CharlotteHill哪個版本的吱吱聲?在4.x系列ZipArchive被分類在壓縮檔案類 –

+0

我使用的是版本2,但我會嘗試升級到4.謝謝! –