Comments (2)
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.
@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)
- Progress when decompression is needed HOT 1
- Memory issues when working with larger files HOT 8
- [CRASH] MsbBitReader.bit() HOT 4
- AR Format Support HOT 7
- Can Gzip unarchive return Member? HOT 1
- GzipArchive.unarchive wrongMagic for short data HOT 1
- Brotli Support? HOT 2
- Symlinks in tarballs are created with absolute paths HOT 3
- BZip2Error.wrongCRC when decompressing HOT 2
- Support File I/O for large archives and resource-constrained environments
- Error when decompressing a certain 7z file HOT 2
- Some zip files can't be extracted HOT 2
- LZ4 format not compatible? HOT 8
- Compiling for watchOS fails HOT 1
- LZ4 decompress speed HOT 10
- Support splitting zips into multiple files
- Is there a way to extract a single file from a 7z compressed archive? HOT 4
- LZFSE Support
- Please update BitByteData dependency HOT 6
- Crash during GzipArchive.unarchive
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swcompression.