GithubHelp home page GithubHelp logo

Comments (2)

tsolomko avatar tsolomko commented on July 19, 2024

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).

from swcompression.

LebJe avatar LebJe commented on July 19, 2024

@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.

from swcompression.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.