eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'ZipArchive':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: ZipArchive


Inheritance:

   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

Description:


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


Class protocol:

Signal constants
o  unsupportedZipFileFormatErrorSignal

o  zipFileFormatErrorSignal

accessing-default
o  defaultAppendTrailingSlash
Returns the default trailing slash behavior. For details
see the class documentation

o  defaultAppendTrailingSlash: aBoolean
Sets the default trailing slash behavior. For details
see the class documentation

o  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
o  initialize
self initialize

cleanup
o  flush
forget about cached zipArchives

usage example(s):

     self flush

o  installFlushBlock
forget about cached zipArchives

usage example(s):

     self installFlushBlock

o  lowSpaceCleanup
forget about cached zipArchives

usage example(s):

     self lowSpaceCleanup

constants
o  COMPRESSION_DEFLATED
please use compressionDeflated instead (Squeak compat.)

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  COMPR_DEFLATED
please use compressionDeflated instead (Squeak compat.)

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  LREC_SIZE

o  centralDirectoryMinimumSize

o  compressionDeflated
same as COMPRESSION_DEFLATED - squeak compatibility

o  compressionStored
same as COMPRESSION_STORED - squeak compatibility

o  streamBufferSize
1024 * 64

debugging
o  debugTrace: aBoolean

instance creation
o  appendFileNamed: name

o  newFileNamed: name

o  oldFileNamed: name

o  oldFileNamed: name startOfArchive: startOfArchive endOfArchive: endOfArchive

o  readingFrom: aPositionableStream
open an existing Zip archive - read data from aPositionableStream

o  writingTo: aPositionableStream
open an new Zip archive - write data to aPositionableStream

queries
o  isZipArchive: aFilename
answer true, if aFilename references a Zip archive

utilities
o  dateToZipFileDate: aDate
data in msdos format

o  timeToZipFileTime: aTime
time in msdos format


Instance protocol:

Compatibility-Squeak
o  binaryContentsOf: fileName

o  contentsOf: fileName
( an extension from the stx:libcompat package )

o  desiredCompressionMethod: aCompressionMethod
for now: ignored

o  testUTF8
( an extension from the stx:libcompat package )

accessing
o  appendTrailingSlash
Returns default trailing slash behavior, For details,
see class documentation

o  appendTrailingSlash: aBoolean
Sets trailing slash behavior. If true, all directory entries
will have a trailing slash in its nama. For details, see class
documentation

o  entries
return a collection of fileName entries

o  file

o  fileSize

o  memberNamed: aFilename

o  members
return a collection of members

o  membersMatching: aFileMatchPattern
return a collection of members which match aFileMatchPattern

o  name
return the (file-)name of this zipArchive

o  numberOfEntries
return the number of entries in the archive

o  pathName
FileStream compatibility: answer the name of the underlying file - a String

o  rawStream

o  setArchiveStartPosition: aStartposition endPosition: anEndPosition

o  signatureInformation
for compatibility with SignedZipArchive

o  size
Modified (format): / 17-02-2017 / 22:30:50 / stefan

o  zipMembersByName

comparing
o  = aZipArchiveToCompare
open both archives
- check file size
- check number of archive members
- perform a binary compare of the archives.

error raising
o  error: anErrorString
redefined, to raise ZipFileFormatErrorSignal

open & close
o  close

o  closePrepareForReopen
close the underlying OS resources (stream),
but keep enough information to allow reopening later (i.e. the archive name)

o  flush
finish the zip archive, but do not close the underlying stream

o  name: name mode: readOrWriteMode
open read or writestream on archiveFileName

o  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) **

o  readingFrom: aPositionableStream
initialize the archive to read from aPositionableStream

o  reopenForReading

o  writingTo: aPositionableStream
initialize the archive to write to aPositionableStream

private
o  closeFile
backward compatibility

** This is an obsolete interface - do not use it (it may vanish in future versions) **

o  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.

o  openFile

o  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.

o  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
o  decode: rawBytes method: compressionMethod size: uncompressedSize
decode rawBytes into a byteArray

o  decode: rawBytes method: compressionMethod size: uncompressedSize asString: asString
decode rawBytes into a byteArray or string

o  inflate: inBytes to: outBytes
bad blockType 2

private - directory stuff
o  addCentralZipDirectory
ensure that the file position is at end

o  addMember: zmemb
add a zipMember

o  checkZipArchive
check if my file is really a zip archive. answer true or false.

o  checkZipArchive: archiveFileName

o  findMember: name
find a zipMember by name

o  findMemberAllowForMissingTrailingSlash: name
find a zipMember by name. Allow for missing trailing slash for directories.
This method is currently used by JavaVM

o  findMemberForWhich: aOneArgBlock
find a zipMember by condition

o  readDirectory
read the zip directory into a linked-list of zipMembers

o  searchForEndOfCentralDirectorySignature
read the zip directory into a linked-list of zipMembers

o  zipMembersDo: aBlock
evaluate aBlock for all zipMembers

queries
o  isValidFile: path
Return true, if the recevier contains given file. false otherwise.

o  isValidPath: anArchivePathName

reading
o  extract: fileName
extract an entry identified by fileName as a byteArray;
nil on errors

o  extract: fileName asString: asString
extract an entry identified by fileName as a byteArray or string;
nil on errors

o  nextBytes: bytesToRead of: zmember startingAt: pos into: b startingAt: off

o  restoreOsDirectory: osDirectoryName fromArchiveDirectory: archiveDirectoryName
extracts all files from an archiveDirectory
into a folder

o  withPositionAndMemberFor: fileName do: aBlock

reading - stream
o  extract: fileName intoStream: aWriteStream
extract an entry identified by filename into aWriteStream

o  readStreamFor: nameOfFileInArchive
open a stream on archive contents identified by nameOfFileInArchive

o  reopenAndExtract: fileName intoStream: aWriteStream
extract an entry identified by filename into aWriteStream

writing
o  addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName

o  addArchiveDirectory: archiveDirectoryName fromOsDirectory: osDirectoryName compressMethod: theCompressMethod
do not create directories (isDirectory = true) - they are not compatible between operating systems

o  addArchiveDirectoryCompressed: archiveDirectoryName fromOsDirectory: osDirectoryName

o  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) **

o  addFile: aFileName fromStream: aStream

o  addFile: aFileName fromStream: aStream compressMethod: theCompressMethodArg

o  addFile: aFileName fromStream: aStream compressMethod: theCompressMethodArg asDirectory: isDirectory
do not create directories (isDirectory = true) - they are not compatible between operating systems

o  addFile: aFileName withContents: data

o  addFile: aFileName withContents: data compressMethod: theCompressMethodArg asDirectory: isDirectory
do not create directories (isDirectory = true) - they are not compatible between operating systems

o  addFileCompressed: aFileName fromStream: aStream

o  addFileCompressed: aFileName withContents: data

o  addString: aString as: path

o  basicAddFile: aFileName withContents: data compressMethod: theCompressMethodArg asDirectory: isDirectory
do not create directories (isDirectory = true) - they are not compatible between operating systems

writing - stream
o  compressedWriteStreamFor: nameOfFileInArchive
create new entry in central directory

o  writeStreamFor: nameOfFileInArchive compressMethod: theCompressMethodArg
create new entry in central directory


Private classes:

    AbstractZipStream
    ZipCentralDirectory
    ZipMember
    ZipReadStream
    ZipWriteStream

Examples:


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


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Fri, 19 Apr 2024 03:53:30 GMT