Skip to content

Commit

Permalink
Update ZIPFolder for compressedSize and uncompressedSize
Browse files Browse the repository at this point in the history
  • Loading branch information
buh committed Oct 4, 2023
1 parent 9c72c2b commit b40e82b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Demo/Hubble/ImagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ struct ImagesView: View {

Spacer()

Text(ByteCountFormatter.appFormatter.string(fromByteCount: folder.size))
Text(ByteCountFormatter.appFormatter.string(fromByteCount: folder.compressedSize))
.foregroundColor(.secondary)
.font(.caption)
}
Expand Down
28 changes: 18 additions & 10 deletions Sources/ZipPinch/ZIPFolder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public struct ZIPFolder: Identifiable, Hashable, Equatable {
public let name: String
public internal(set) var entries: [ZIPEntry] = []
public internal(set) var subfolders: [Self] = []
public internal(set) var size: Int64 = 0
public internal(set) var compressedSize: Int64 = 0
public internal(set) var uncompressedSize: Int64 = 0
public internal(set) var lastModificationDate: Date = .msDOSReferenceDate
}

Expand Down Expand Up @@ -57,21 +58,28 @@ public extension [ZIPEntry] {
}

rootFolder.subfolders.appendEntry(entry, at: indices)
rootFolder.findLastModificationDate()
}

rootFolder.calcSize()
rootFolder.calcSize(isCompressedSize: true)
rootFolder.calcSize(isCompressedSize: false)
rootFolder.findLastModificationDate()
return rootFolder
}
}

private extension ZIPFolder {
@discardableResult
mutating func calcSize() -> Int64 {
let entriesSize = entries.reduce(0) { $0 + $1.compressedSize }
let subfoldersSize = subfolders.foldersSize()
size = entriesSize + subfoldersSize
return size
mutating func calcSize(isCompressedSize: Bool) -> Int64 {
let entriesSize = entries.reduce(0) { $0 + (isCompressedSize ? $1.compressedSize : $1.uncompressedSize) }
let subfoldersSize = subfolders.foldersSize(isCompressedSize: isCompressedSize)

if isCompressedSize {
compressedSize = entriesSize + subfoldersSize
} else {
uncompressedSize = entriesSize + subfoldersSize
}

return isCompressedSize ? compressedSize : uncompressedSize
}

@discardableResult
Expand Down Expand Up @@ -132,11 +140,11 @@ private extension [ZIPFolder] {
}
}

mutating func foldersSize() -> Int64 {
mutating func foldersSize(isCompressedSize: Bool) -> Int64 {
var size: Int64 = 0

for index in 0..<count {
size += self[index].calcSize()
size += self[index].calcSize(isCompressedSize: isCompressedSize)
}

return size
Expand Down

0 comments on commit b40e82b

Please sign in to comment.