{"id":21310492,"url":"https://github.com/lebje/cpioarchivekit","last_synced_at":"2025-07-11T23:31:49.359Z","repository":{"id":48517005,"uuid":"368025359","full_name":"LebJe/CPIOArchiveKit","owner":"LebJe","description":"A simple, 0-dependency Swift package for reading and writing cpio archives.","archived":false,"fork":false,"pushed_at":"2024-01-06T00:48:16.000Z","size":16252,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-17T03:36:18.430Z","etag":null,"topics":["cpio","cpio-archives","linux","macos","swift","swift-library","swift5","windows"],"latest_commit_sha":null,"homepage":"https://lebje.github.io/CPIOArchiveKit/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LebJe.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-05-17T01:46:50.000Z","updated_at":"2024-08-29T16:33:22.000Z","dependencies_parsed_at":"2024-01-06T01:47:15.235Z","dependency_job_id":"7b83fbdd-bdc3-4b45-a7b3-8e19b51080e6","html_url":"https://github.com/LebJe/CPIOArchiveKit","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"cfd9cae28d2864dea0533f137397ca8124767917"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LebJe%2FCPIOArchiveKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LebJe%2FCPIOArchiveKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LebJe%2FCPIOArchiveKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LebJe%2FCPIOArchiveKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LebJe","download_url":"https://codeload.github.com/LebJe/CPIOArchiveKit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225769345,"owners_count":17521258,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpio","cpio-archives","linux","macos","swift","swift-library","swift5","windows"],"created_at":"2024-11-21T17:13:51.321Z","updated_at":"2024-11-21T17:13:51.981Z","avatar_url":"https://github.com/LebJe.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPIOArchiveKit\n\n**A simple, 0-dependency Swift package for reading and writing [`cpio`](https://en.wikipedia.org/wiki/Cpio) archives. Inspired by [go-cpio](https://github.com/cavaliercoder/go-cpio).**\n\n[![Swift 5.3](https://img.shields.io/badge/Swift-5.3-brightgreen?logo=swift)](https://swift.org)\n[![SPM Compatible](https://img.shields.io/badge/SPM-compatible-brightgreen.svg)](https://swift.org/package-manager)\n[![](https://img.shields.io/github/v/tag/LebJe/CPIOArchiveKit)](https://github.com/LebJe/CPIOArchiveKit/releases)\n[![Build and Test](https://github.com/LebJe/CPIOArchiveKit/workflows/Build%20and%20Test/badge.svg)](https://github.com/LebJe/CPIOArchiveKit/actions?query=workflow%3A%22Build+and+Test%22)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FLebJe%2FCPIOArchiveKit%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/LebJe/CPIOArchiveKit)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FLebJe%2FCPIOArchiveKit%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/LebJe/CPIOArchiveKit)\n\n# Table of Contents\n\n\u003c!--ts--\u003e\n\n-   [CPIOArchiveKit](#cpioarchivekit)\n-   [Table of Contents](#table-of-contents)\n    -   [Installation](#installation)\n        -   [Swift Package Manager](#swift-package-manager)\n    -   [Usage](#usage)\n        -   [Writing Archives](#writing-archives)\n            -   [Checksums](#checksums)\n                -   [Computed Checksum](#computed-checksum)\n                -   [Pre-computed Checksum](#pre-computed-checksum)\n        -   [Reading Archives](#reading-archives)\n            -   [Iteration](#iteration)\n            -   [Subscript](#subscript)\n            -   [FileMode](#filemode)\n                -   [File Type](#file-type)\n                -   [Permissions](#permissions)\n    -   [Examples](#examples)\n    -   [Other Platforms](#other-platforms)\n        -   [Windows](#windows)\n    -   [Contributing](#contributing)\n\n\u003c!-- Added by: lebje, at: Wed Jul 21 10:15:16 EDT 2021 --\u003e\n\n\u003c!--te--\u003e\n\nCreated by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)\n\nDocumentation is available [here](https://lebje.github.io/CPIOArchiveKit).\n\n## Installation\n\n### Swift Package Manager\n\nAdd this to the `dependencies` array in `Package.swift`:\n\n```swift\n.package(url: \"https://github.com/LebJe/CPIOArchiveKit.git\", from: \"0.0.2\")\n```\n\nAlso add this to the `targets` array in the aforementioned file:\n\n```swift\n.product(name: \"CPIOArchiveKit\", package: \"CPIOArchiveKit\")\n```\n\n## Usage\n\n### Writing Archives\n\nTo write archives, you'll need a `CPIOArchiveWriter`:\n\n```swift\nvar writer = CPIOArchiveWriter()\n```\n\nOnce you have your writer, you must create a `Header`, that describes the file you wish to add to your archive:\n\n```swift\nvar time: Int = 1615929568\n\n// You can also use date\nlet date: Date = ...\ntime = Int(date.timeIntervalSince1970)\n\n// File\nlet header = Header(\n   name: \"hello.txt\",\n   mode: FileMode(0o644, modes: [.regular]),\n   modificationTime: time\n)\n\n// Directory\nlet header = Header(\n   name: \"dir/\",\n   mode: FileMode(0o644, modes: [.directory]),\n   modificationTime: time\n)\n```\n\n#### Checksums\n\nIf you would like to provide a cpio checksum with the `Header` you created above, there are two ways to do so.\n\n##### Computed Checksum\n\n```swift\n// Compute the checksum.\nlet fileContents: [UInt8] = ...\nlet checksum = Checksum(bytes: fileContents)\n```\n\n##### Pre-computed Checksum\n\n```swift\nlet preComputedChecksum = 123\nlet checksum = Checksum(sum: preComputedChecksum)\nl\n```\n\nOnce you have a `checksum`, add it to the checksum parameter of your `Header`:\n\n```swift\nHeader(..., checksum: checksum, ...)\n```\n\nOnce you have your `Header`, you can write it, along with the contents of your file, to the archive:\n\n```swift\n// Without Foundation\nvar contents = Array(\"Hello\".utf8)\n\n// With Foundation\nlet myData: Data = \"Hello\".data(using .utf8)!\ncontents = Array\u003cUInt8\u003e(myData)\n\nwriter.addFile(header: header, contents: contents)\n```\n\nIf you have a text file, use the overloaded version of `addFile`:\n\n```swift\nwriter.addFile(header: header, contents: \"Hello\")\n```\n\n\u003e For directories, omit the `contents` parameter in `addFile`. For symlinks, set the `contents` parameter to the file or directory the link points to.\n\nOnce you have added your files, you must call `finalize()` to create the archive and return the data:\n\n```swift\n// The binary representation (Array\u003cUInt8\u003e) of the created archive.\nlet bytes = writer.finalize()\n\n// You convert it to `Data` like this:\nlet data = Data(bytes)\n\n// And write it:\ntry data.write(to: URL(fileURLWithPath: \"myArchive.cpio\"))\n```\n\nif you want to reuse `writer`, call `finalize(clear: true)` instead, which will clear the state inside `writer`.\n\n### Reading Archives\n\nTo read archives, you need an `CPIOArchiveReader`:\n\n```swift\n// myData is the bytes of the archive.\nlet myData: Data = ...\n\nlet reader = CPIOArchiveReader(archive: Array\u003cUInt8\u003e(myData))\n```\n\nOnce you have your reader, there are several ways you can retrieve the data:\n\n#### Iteration\n\nYou can iterate though all the files in the archive like this:\n\n```swift\nfor (header, data) in reader {\n   // `data` is `Array\u003cUInt8\u003e` that contains the raw bytes of the file in the archive.\n   // `header` is the `Header` that describes the `data`.\n\n   // if you know `data` is a `String`, then you can use this initializer:\n   let str = String(data)\n}\n```\n\n#### Subscript\n\nAccessing data through the `subscript` is useful when you only need to access a few items in a large archive:\n\n```swift\n\n// The subscript provides you with random access to any file in the archive:\nlet firstFile = reader[0]\nlet fifthFile = reader[6]\n```\n\nYou can also use the version of the subscript that takes a `Header` - useful for when you have a `Header`, but not the index of that header.\n\n```swift\nlet header = reader.headers.first(where: { $0.name.contains(\".swift\") })!\nlet data = reader[header: header]\n```\n\n#### `FileMode`\n\nOnce you have retrived a `FileMode` from a `Header` in a `CPIOArchiveReader`, you can access the file's type and UNIX permissions.\n\n#### File Type\n\n```swift\nlet type = header.mode.type\n\nswitch type {\n\tcase .regular: // Regular file.\n\tcase .directory: // Directory.\n\tcase .symlink: // Symbolic link.\n\t...\n}\n```\n\n#### Permissions\n\nTo access the UNIX permissions, use the `permissions` variable in `FileMode`.\n\n## Examples\n\n-   `Examples/CreateReadWrite`: This example shows how to use CPIOArchiveKit to read, create, or extract an archive; or compute a checksum for an archive using only `Darwin.C` (macOS), `Glibc` (Linux) or `ucrt` (Windows (not tested)).\n\n## Other Platforms\n\nCPIOArchiveKit doesn't depend on any library, `Foundation`, or `Darwin`/`Glibc`/`ucrt` - only the Swift standard library. It should compile on any platform where the standard library compiles.\n\n### Windows\n\nCPIOArchiveKit is currently being built on Windows, but not tested, as the [Swift Package Manager Resources](https://github.com/apple/swift-evolution/blob/main/proposals/0271-package-manager-resources.md) doesn't seem to work (or isn't available) on Windows.\n\n## Contributing\n\nBefore committing, please install [pre-commit](https://pre-commit.com), [swift-format](https://github.com/nicklockwood/SwiftFormat), and [Prettier](https://prettier.io), then install the pre-commit hook:\n\n```bash\n$ brew bundle # install the packages specified in Brewfile\n$ pre-commit install\n\n# Commit your changes.\n```\n\nTo install pre-commit on other platforms, refer to the [documentation](https://pre-commit.com/#install).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flebje%2Fcpioarchivekit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flebje%2Fcpioarchivekit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flebje%2Fcpioarchivekit/lists"}