TarWriter
public struct TarWriter
A type that allows to iteratively create TAR containers with the output being written into a FileHandle.
The TarWriter may be helpful in reducing the peak memory usage on certain platforms. However, to achieve this both
the creation of TAR entries and the calls to TarWriter should be wrapped inside the autoreleasepool. Since the
autoreleasepool is available only on Darwin platforms, the memory reducing effect may be not as significant on
non-Darwin platforms (such as Linux or Windows).
The following code demonstrates an example usage of the TarWriter:
let handle: FileHandle = ...
let writer = TarWriter(fileHandle: handle)
try autoreleasepool {
let entry: TarEntry = ...
try writer.append(entry)
}
try writer.finalize()
try handle.close()
Note that TarWriter.finalize() must be called after finishing appending entries to the container. In addition,
closing the FileHandle remains the responsibility of the caller.
Important
Due to the API availability limitations of Foundation’sFileHandle, on certain platforms errors in
FileHandle operations may result in unrecoverable runtime failures due to unhandled Objective-C exceptions (which are
impossible to correctly handle in Swift code). As such, it is not recommended to use TarWriter on those platforms.
The following platforms are unaffected by this issue: macOS 10.15.4+, iOS 13.4+, watchOS 6.2+, tvOS 13.4+, and any
other platforms without Objective-C runtime.
-
Creates a new instance for writing TAR entries using the specified
formatinto the providedfileHandle.The
TarWriterwill be forced to use the providedformat, meaning that certain properties of theentriesmay be missing from the output data if the chosen format does not support corresponding features. The default.paxformat supports the largest set of features. Other (non-PAX) formats should only be used if you have a specific need for them and you understand limitations of those formats.Important
TarWriter.finalize()must be called after all entries have been appended.Declaration
Swift
public init(fileHandle: FileHandle, force format: TarContainer.Format = .pax)Parameters
fileHandleA handle into which the output will be written. Note that the
TarWriterdoes not close thefileHandleand this remains the responsibility of the caller.forceForce the usage of the specified format.
-
Adds a new TAR entry at the end of the TAR container.
On Darwin platforms it is recommended to wrap both the initialization of a
TarEntryand the call to this function inside theautoreleasepoolin order to reduce the peak memory usage.Throws
Errors from theFileHandleoperations.Declaration
Swift
public mutating func append(_ entry: TarEntry) throws -
Finalizes the TAR container by adding an EOF marker.
Throws
Errors from theFileHandleoperations.Declaration
Swift
public func finalize() throws
View on GitHub
TarWriter Structure Reference