|
Class: ZipArchive
Object
|
+--ZipArchive
- Package:
- stx:libbasic2
- Category:
- System-Support-FileFormats
- Version:
- rev:
1.152
date: 2019/07/08 14:37:07
- user: cg
- file: ZipArchive.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
- Author:
- Claus Gittinger
provides access to a zip archive.
Trailing slash.
Some implementations require a trailing slash in directory
names (such as the OpenOffice zip implementation). Others just
ignore external file attributes and indicate a directory entry
by adding a trailing slash (such as the Java zip implementation).
Since ZipArchive 1.98 a trailing slash is added for all directory
entries iff appendTrailingSlash instvar is set to true. By default
it is set to the value of DefaultAppendTrailingSlash which defaults
to true.
Setting appendTrailingSlash to false inhibits trailing slash
behavior.
Caveat:
the only compression methods (for now) are store and deflate.
[classvars:]
DefaultAppendTrailingSlash...a default value for appendTralingSlash instvar.
For details, see above
Signal constants
-
unsupportedZipFileFormatErrorSignal
-
-
zipFileFormatErrorSignal
-
accessing-default
-
defaultAppendTrailingSlash
-
Returns the default trailing slash behavior. For details
see the class documentation
-
defaultAppendTrailingSlash: aBoolean
-
Sets the default trailing slash behavior. For details
see the class documentation
-
zipFileCachingTime: seconds
-
by default, zip files are cached for some time,
in case they are reconsulted soon.
The default time is 60s, but can be changed by this setter
class initialization
-
initialize
-
self initialize
cleanup
-
flush
-
forget about cached zipArchives
usage example(s):
-
installFlushBlock
-
forget about cached zipArchives
usage example(s):
-
lowSpaceCleanup
-
forget about cached zipArchives
usage example(s):
constants
-
COMPRESSION_DEFLATED
-
please use compressionDeflated instead (Squeak compat.)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
COMPR_DEFLATED
-
please use compressionDeflated instead (Squeak compat.)
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
LREC_SIZE
-
-
centralDirectoryMinimumSize
-
-
compressionDeflated
-
same as COMPRESSION_DEFLATED - squeak compatibility
-
compressionStored
-
same as COMPRESSION_STORED - squeak compatibility
-
streamBufferSize
-
1024 * 64
debugging
-
debugTrace: aBoolean
-
instance creation
-
appendFileNamed: name
-
-
newFileNamed: name
-
-
oldFileNamed: name
-
-
oldFileNamed: name startOfArchive: startOfArchive endOfArchive: endOfArchive
-
-
readingFrom: aPositionableStream
-
open an existing Zip archive - read data from aPositionableStream
-
writingTo: aPositionableStream
-
open an new Zip archive - write data to aPositionableStream
queries
-
isZipArchive: aFilename
-
answer true, if aFilename references a Zip archive
utilities
-
dateToZipFileDate: aDate
-
data in msdos format
-
timeToZipFileTime: aTime
-
time in msdos format
Compatibility-Squeak
-
binaryContentsOf: fileName
-
-
contentsOf: fileName ( an extension from the stx:libcompat package )
-
-
desiredCompressionMethod: aCompressionMethod
-
for now: ignored
-
testUTF8 ( an extension from the stx:libcompat package )
-
accessing
-
appendTrailingSlash
-
Returns default trailing slash behavior, For details,
see class documentation
-
appendTrailingSlash: aBoolean
-
Sets trailing slash behavior. If true, all directory entries
will have a trailing slash in its nama. For details, see class
documentation
-
entries
-
return a collection of fileName entries
-
file
-
-
fileSize
-
-
memberNamed: aFilename
-
-
members
-
return a collection of members
-
membersMatching: aFileMatchPattern
-
return a collection of members which match aFileMatchPattern
-
name
-
return the (file-)name of this zipArchive
-
numberOfEntries
-
return the number of entries in the archive
-
pathName
-
FileStream compatibility: answer the name of the underlying file - a String
-
rawStream
-
-
setArchiveStartPosition: aStartposition endPosition: anEndPosition
-
-
signatureInformation
-
for compatibility with SignedZipArchive
-
size
-
Modified (format): / 17-02-2017 / 22:30:50 / stefan
-
zipMembersByName
-
comparing
-
= aZipArchiveToCompare
-
open both archives
- check file size
- check number of archive members
- perform a binary compare of the archives.
error raising
-
error: anErrorString
-
redefined, to raise ZipFileFormatErrorSignal
open & close
-
close
-
-
closePrepareForReopen
-
close the underlying OS resources (stream),
but keep enough information to allow reopening later (i.e. the archive name)
-
flush
-
finish the zip archive, but do not close the underlying stream
-
name: name mode: readOrWriteMode
-
open read or writestream on archiveFileName
-
readFrom: aPositionableStreamOrFilenameString
-
initialize the archive to read from aPositionableStream,
or from the file by that name (pharo compatibility)
Obsolete - backward compatibility.
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
readingFrom: aPositionableStream
-
initialize the archive to read from aPositionableStream
-
reopenForReading
-
-
writingTo: aPositionableStream
-
initialize the archive to write to aPositionableStream
private
-
closeFile
-
backward compatibility
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
dataStartOf: zipEntry
-
fetch the absolute start address of the data of a given zipEntry.
Note: extra field and extra field length may be different from that in
the central directory entry. Sow e have to fetch the local header.
-
openFile
-
-
setDefaultArchiveBounds
-
set start and end of archive if it is nil. That means no bounds have been defined
before. In that case the archive is the complete file.
-
validZipFileNameFrom: zipFileName
-
ZipArchive new validZipFileNameFrom:'hello//world'
ZipArchive new validZipFileNameFrom:'hello\\world'
ZipArchive new validZipFileNameFrom:'hello\/world'
ZipArchive new validZipFileNameFrom:'hello/\world'
ZipArchive new validZipFileNameFrom:'hello/\world/aaa bbb/ccc'
private - decompression
-
decode: rawBytes method: compressionMethod size: uncompressedSize
-
decode rawBytes into a byteArray
-
decode: rawBytes method: compressionMethod size: uncompressedSize asString: asString
-
decode rawBytes into a byteArray or string
-
inflate: inBytes to: outBytes
-
bad blockType 2
private - directory stuff
-
addCentralZipDirectory
-
ensure that the file position is at end
-
addMember: zmemb
-
add a zipMember
-
checkZipArchive
-
check if my file is really a zip archive. answer true or false.
-
checkZipArchive: archiveFileName
-
-
findMember: name
-
find a zipMember by name
-
findMemberAllowForMissingTrailingSlash: name
-
find a zipMember by name. Allow for missing trailing slash for directories.
This method is currently used by JavaVM
-
findMemberForWhich: aOneArgBlock
-
find a zipMember by condition
-
readDirectory
-
read the zip directory into a linked-list of zipMembers
-
searchForEndOfCentralDirectorySignature
-
read the zip directory into a linked-list of zipMembers
-
zipMembersDo: aBlock
-
evaluate aBlock for all zipMembers
queries
-
isValidFile: path
-
Return true, if the recevier contains given file. false otherwise.
-
isValidPath: anArchivePathName
-
reading
-
extract: fileName
-
extract an entry identified by fileName as a byteArray;
nil on errors
-
extract: fileName asString: asString
-
extract an entry identified by fileName as a byteArray or string;
nil on errors
-
nextBytes: bytesToRead of: zmember startingAt: pos into: b startingAt: off
-
-
restoreOsDirectory: osDirectoryName fromArchiveDirectory: archiveDirectoryName
-
extracts all files from an archiveDirectory
into a folder
-
withPositionAndMemberFor: fileName do: aBlock
-
reading - stream
-
extract: fileName intoStream: aWriteStream
-
extract an entry identified by filename into aWriteStream
-
readStreamFor: nameOfFileInArchive
-
open a stream on archive contents identified by nameOfFileInArchive
-
reopenAndExtract: fileName intoStream: aWriteStream
-
extract an entry identified by filename into aWriteStream
writing
-
addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName
-
-
addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName compressMethod: theCompressMethod
-
do not create directories (isDirectory = true) - they are not compatible between operating systems
-
addArchiveDirectoryCompressed: archiveDirectoryName fromOsDirectory: osDirectoryName
-
-
addDirectory: aDirectoryName
-
do not create directories (isDirectory = true) - they are not compatible between operating systems
** This is an obsolete interface - do not use it (it may vanish in future versions) **
-
addFile: aFileName fromStream: aStream
-
-
addFile: aFileName fromStream: aStream compressMethod: theCompressMethodArg
-
-
addFile: aFileName fromStream: aStream compressMethod: theCompressMethodArg asDirectory: isDirectory
-
do not create directories (isDirectory = true) - they are not compatible between operating systems
-
addFile: aFileName withContents: data
-
-
addFile: aFileName withContents: data compressMethod: theCompressMethodArg asDirectory: isDirectory
-
do not create directories (isDirectory = true) - they are not compatible between operating systems
-
addFileCompressed: aFileName fromStream: aStream
-
-
addFileCompressed: aFileName withContents: data
-
-
addString: aString as: path
-
-
basicAddFile: aFileName withContents: data compressMethod: theCompressMethodArg asDirectory: isDirectory
-
do not create directories (isDirectory = true) - they are not compatible between operating systems
writing - stream
-
compressedWriteStreamFor: nameOfFileInArchive
-
create new entry in central directory
-
writeStreamFor: nameOfFileInArchive compressMethod: theCompressMethodArg
-
create new entry in central directory
AbstractZipStream
ZipCentralDirectory
ZipMember
ZipReadStream
ZipWriteStream
|zip bytes|
zip := ZipArchive oldFileNamed:'foo.zip'.
bytes := zip extract:'bar'.
zip close.
|
|zip bytes|
zip := ZipArchive oldFileNamed:'source/stx/libbasic2.zip'.
zip entries do:[:entry |
Transcript showCR:entry
].
zip close.
|
|zip bytes|
zip := ZipArchive oldFileNamed:'source/stx/libbasic2.zip'.
bytes := zip extract:'TwoByteStr.st'.
zip close.
Transcript showCR:(bytes asString).
|
compatibility write check with winzip (compressed with deflate)
|zipwr testDirectory testFileWr|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileWr := 'crcTest_resume_compressed.zip'.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
zipwr addFile:'crcTest_resume_compressed.txt' withContents: 'resume'.
zipwr close.
|
compatibility read check with winzip (compressed with deflate)
|ziprd testDirectory testFileRd contents|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileRd := 'crcTest_resume_compressed.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
contents := ziprd extract: ziprd entries first.
contents inspect.
ziprd close.
|
compatibility write check with winzip (uncompressed)
|zipwr testDirectory testFileWr|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileWr := 'crcTest_resume_uncompressed.zip'.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
zipwr addFile:'crcTest_resume_uncompressed.txt'
withContents:'resume'
compressMethod:0
asDirectory:false.
zipwr close.
|
compatibility read check with winzip (uncompressed)
|ziprd testDirectory testFileRd contents|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileRd := 'crcTest_resume_uncompressed.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
contents := ziprd extract: ziprd entries first.
contents inspect.
ziprd close.
|
read an archive with files and/or directories, fetch the entries
and create a new archive with the same content
|ziprd zipwr entryDict testDirectory testFileRd testFileWr|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileRd := 'projects.zip'.
testFileWr := 'projects_expecco_test.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
entryDict := Dictionary new.
ziprd entries do: [:aFileName|
entryDict at:aFileName put:(ziprd extract: aFileName) asString.
].
ziprd close.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
entryDict keysAndValuesDo: [:key :value|
(value size == 0) ifTrue: [
zipwr addDirectory:key.
] ifFalse: [
zipwr addFile:key withContents:value
].
].
zipwr close.
|
|zipwr ziprd testDirectory testFileWr testFileRd zs|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileWr := 'crcTest_resume_compressed.zip'.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
zipwr addFile:'crcTest_resume_compressed.txt' withContents: 'Das ist ein test, das ist ein test, das ist ein test'.
zipwr close.
testFileRd := 'crcTest_resume_compressed.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
zs := ziprd readStreamFor: 'crcTest_resume_compressed.txt'.
zs inspect.
ziprd close.
|
|zipwr ziprd testDirectory testFileWr testFileRd rs result|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileWr := 'readStreamTest_HelloWorld.zip'.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
zipwr addFile:'readStreamTest_HelloWorld.txt' withContents: 'Hello World!' compressed: false.
zipwr close.
testFileRd := 'readStreamTest_HelloWorld.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
rs := ziprd readStreamFor: 'readStreamTest_HelloWorld.txt'.
result := ''.
[ rs atEnd ] whileFalse: [
result := result, (rs nextAvailable:5).
].
result inspect.
rs close.
ziprd close.
|
read an archive with files and/or directories and/or zipArchives,
fetch the entries (also from the include zip archives)
and create a new archive
|ziprd zipwr entryDict testDirectory testFileRd testFileWr zipRdSub1 zipRdSub2|
testDirectory := 'C:\Dokumente und Einstellungen\stefan\Eigene Dateien\tmp\'.
testFileRd := 'ZipInZipFileTest.zip'.
testFileWr := 'ZipInZipFileTest_generated.zip'.
ziprd := ZipArchive oldFileNamed:(testDirectory, testFileRd).
entryDict := Dictionary new.
ziprd entries do: [:aFileName|
Transcript showCR: 'processing in top: ', aFileName.
(aFileName endsWith:'.zip') ifTrue: [
zipRdSub1 := ziprd extractArchive: aFileName.
zipRdSub1 entries do: [:aFileName1|
Transcript showCR: 'processing in sub 1: ', aFileName1.
(aFileName1 endsWith:'.zip') ifTrue: [
zipRdSub2 := zipRdSub1 extractArchive: aFileName1.
zipRdSub2 entries do: [:aFileName2|
Transcript showCR: 'processing in sub 2: ', aFileName2.
(aFileName2 endsWith:'.zip') ifTrue: [
self halt.
] ifFalse: [
entryDict at:aFileName2 put:(zipRdSub2 extract: aFileName2) asString.
].
].
zipRdSub2 close.
] ifFalse: [
entryDict at:aFileName1 put:(zipRdSub1 extract: aFileName1) asString.
].
].
zipRdSub1 close.
] ifFalse: [
entryDict at:aFileName put:(ziprd extract: aFileName) asString.
].
].
ziprd close.
zipwr := ZipArchive newFileNamed:(testDirectory, testFileWr).
entryDict keysAndValuesDo: [:key :value|
(value size == 0) ifTrue: [
zipwr addDirectory:key.
] ifFalse: [
zipwr addFile:key withContents:value
].
].
zipwr close.
|
|