Skip to content

Commit

Permalink
Open read stream lazily and close it after use + test
Browse files Browse the repository at this point in the history
  • Loading branch information
guillep committed Dec 9, 2020
1 parent 71cad16 commit cbf4e9d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
23 changes: 23 additions & 0 deletions src/Compression-Tests/ZipArchiveTest.class.st
Expand Up @@ -55,6 +55,29 @@ ZipArchiveTest >> testAddNonExistentFile [
self should: [ zip addFile: 'it_would_be_crazy_if_this_file_existed.ext' asFileReference ] raise: FileDoesNotExistException.
]

{ #category : #tests }
ZipArchiveTest >> testArchiveWithThousandFilesShouldWork [

"Smoke test showing that we can create zip archives with up to 1000 elements.
This test was created as a regression test. Previous implementations were leaking files and reaching the open file limits from the operating system."

| dir theZip |
dir := (FileLocator temp / 'testfiles') ensureCreateDirectory.
[
1 to: 1000 do: [ :i |
(dir / ('test-' , i asString))
writeStreamDo: [ :stream | stream nextPutAll: 'file contents' ];
yourself ].

theZip := ZipArchive new.
dir children do: [ :eachFile | theZip addFile: eachFile resolve ].
theZip writeToFile: FileLocator temp / 'result.zip'.
theZip close.
] ensure: [
dir ensureDeleteAll.
(FileLocator temp / 'result.zip') ensureDelete. ]
]

{ #category : #tests }
ZipArchiveTest >> testCanUnzipFromFileName [
| nestedFileToZip |
Expand Down
16 changes: 8 additions & 8 deletions src/Compression/ZipNewFileMember.class.st
Expand Up @@ -22,12 +22,18 @@ ZipNewFileMember >> close [
stream ifNotNil:[stream close].
]

{ #category : #initialization }
ZipNewFileMember >> endRead [

super endRead.
self close
]

{ #category : #initialization }
ZipNewFileMember >> from: aFileReference [

| entry |
"Now get the size, attributes, and timestamps, and see if the file exists"
stream := aFileReference binaryReadStream.
self localFileName: (externalFileName := aFileReference path pathString).
entry := aFileReference entry.
compressedSize := uncompressedSize := entry size.
Expand All @@ -48,16 +54,10 @@ ZipNewFileMember >> initialize [

{ #category : #private }
ZipNewFileMember >> readRawChunk: n [
stream ifNil: [ stream := externalFileName asFileReference binaryReadStream ].
^stream next: n
]

{ #category : #'private-writing' }
ZipNewFileMember >> rewindData [
super rewindData.
readDataRemaining := stream size.
stream position: 0.
]

{ #category : #testing }
ZipNewFileMember >> usesFile: aFileReferenceOrFileName [
"Do I require aFileName? That is, do I care if it's clobbered?"
Expand Down

0 comments on commit cbf4e9d

Please sign in to comment.