From 2e3867afde11e01734004fa0d20bd2b549d68dd6 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 17 Mar 2018 15:34:12 +0300 Subject: [PATCH] [7-Zip] Some code and comments improvements to open and info functions --- Sources/7-Zip/7zContainer.swift | 77 ++++++++++++++------------------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/Sources/7-Zip/7zContainer.swift b/Sources/7-Zip/7zContainer.swift index eaf60301..b77515a2 100644 --- a/Sources/7-Zip/7zContainer.swift +++ b/Sources/7-Zip/7zContainer.swift @@ -27,10 +27,8 @@ public class SevenZipContainer: Container { */ public static func open(container data: Data) throws -> [SevenZipEntry] { var entries = [SevenZipEntry]() - guard let header = try readHeader(data) - else { return [] } - - guard let files = header.fileInfo?.files + guard let header = try readHeader(data), + let files = header.fileInfo?.files else { return [] } /// Total count of non-empty files. Used to iterate over SubstreamInfo. @@ -40,7 +38,7 @@ public class SevenZipContainer: Container { var folderIndex = 0 /// Index of currently extracted file in `headerInfo.fileInfo.files`. - var fileInFolderCount = 0 + var folderFileIndex = 0 /// Index of currently read stream. var streamIndex = -1 @@ -52,21 +50,18 @@ public class SevenZipContainer: Container { var folderCRC = CheckSums.crc32(Data()) /// `ByteReader` object with unpacked stream's data. - var rawFileData = ByteReader(data: Data()) + var unpackedStreamData = ByteReader(data: Data()) let byteReader = ByteReader(data: data) - for fileIndex in 0..= folder.numUnpackSubstreams { // If we read all files in folder... - // We need to check folder's unpacked size as well as its CRC32 (if it is available). + if folderFileIndex >= folder.numUnpackSubstreams { // If we read all files in folder... + // Check folder's unpacked size as well as its CRC32 (if it is available). guard folderUnpackSize == folder.unpackSize() else { throw SevenZipError.wrongSize } if let storedFolderCRC = folder.crc { guard folderCRC == storedFolderCRC else { throw SevenZipError.wrongCRC } } - // Resetting folder's crc and unpack size. + // Reset folder's unpack size and CRC32. folderCRC = CheckSums.crc32(Data()) folderUnpackSize = 0 - // Resetting files count for the next folder. - fileInFolderCount = 0 - // Moving to the next folder. + // Reset file index for the next folder. + folderFileIndex = 0 + // Move to the next folder. folderIndex += 1 } } else { @@ -191,10 +181,8 @@ public class SevenZipContainer: Container { */ public static func info(container data: Data) throws -> [SevenZipEntryInfo] { var entries = [SevenZipEntryInfo]() - guard let header = try readHeader(data) - else { return [] } - - guard let files = header.fileInfo?.files + guard let header = try readHeader(data), + let files = header.fileInfo?.files else { return [] } var nonEmptyFileIndex = 0 @@ -215,7 +203,6 @@ public class SevenZipContainer: Container { } private static func readHeader(_ data: Data) throws -> SevenZipHeader? { - /// Object with input data which supports convenient work with bit shifts. let bitReader = MsbBitReader(data: data) // **SignatureHeader**