-
Hi @tsolomko! I was wondering if it is possible to create a TAR archive from a directory using SWCompression. The example below shows how this could be done using $ tar -cf archive.tar archive-folder Right now I have this code: // Both lines to add the folder.
let entry = TarContainer.Entry(info: .init(name: "folder-name", type: .directory), data: nil)
let entry = TarContainer.Entry(info: .init(name: "/path/to/folder-name", type: .directory), data: nil)
let data = try TarContainer.create(from: [entry]) Unfortunately, when I unarchive the Jeff |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi @LebJe, Yes, it is possible to create a TAR archive, and it is done almost exactly like you did it: import Foundation
import SWCompression
// Both lines to add the folder.
let entry = TarContainer.Entry(info: .init(name: "folder-name", type: .directory), data: nil)
// let entry = TarContainer.Entry(info: .init(name: "/path/to/folder-name", type: .directory), data: nil)
let data = try TarContainer.create(from: [entry])
try data.write(to: URL(fileURLWithPath: "test.tar")) I am not exactly sure what do you mean by "invalid directory". I think, the issue that you've encountered is that the directory that you've created and archived has no attributes set, so it has no file permissions, modification time, etc. When you unpack such TAR archive it produces a directory that looks and behaves weirdly. For example, to delete it you need admin rights. To fix this immediate issue, I would suggest doing like this: // Both lines to add the folder.
var info = TarEntryInfo(name: "folder-name", type: .directory)
info.permissions = Permissions(rawValue: 0o774)
let entry = TarContainer.Entry(info: info, data: nil) I think the misunderstanding here is that SWCompression does not interact with file system at all, so you need to tell it everything (or at least the important stuff) about the files and directory that you're archiving. In the most general case, when you're archiving an actual directory and/or files from your file system you should do it like in this example (from line 75 onwards). |
Beta Was this translation helpful? Give feedback.
-
@tsolomko Thank you for the solution. My code now looks like this: import SWCompression
import Foundation
// From https://github.com/tsolomko/SWCompression/blob/4fa4adcf5bbf263388edcbf2631987039a84b389/Sources/swcomp/Containers/TarCommand.swift#L101
func createEntries(_ inputPath: String) throws -> [TarEntry] {
let inputURL = URL(fileURLWithPath: inputPath)
let fileManager = FileManager.default
...
}
let entries = try createEntries("test-folder")
let data = try GzipArchive.archive(data: try TarContainer.create(from: entries))
try data.write(to: URL(fileURLWithPath: "test-folder.tar.gz")) By "invalid directory" I meant there was nothing in the unarchived directory even though the original folder ("test-folder") had three files in it. With this code the files are in the unarchived directory. |
Beta Was this translation helpful? Give feedback.
@tsolomko Thank you for the solution. My code now looks like this:
By "invalid directory" I meant there was nothing in the unarchived directory even though th…