<!--
{
  "documentType" : "article",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/compressing-file-system-directories",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Compressing file system directories"
}
-->

# Compressing file system directories

Compress the contents of an entire directory and store the result on the file system.

## Discussion

In this article, you’ll learn how to use AppleArchive to compress the contents of an entire directory to a single archive file.

The code below compresses the contents of a directory name `src` using the <doc://com.apple.documentation/documentation/Compression/Algorithm/lzfse> algorithm, and stores the result in a file named `directory.aar`.

### Create the file stream to write the compressed file

Use <doc://com.apple.documentation/documentation/AppleArchive/ArchiveByteStream/fileStream(path:mode:options:permissions:)> to create the file stream that writes the compressed file to the file system. In this case, use the <doc://com.apple.documentation/documentation/System/FileDescriptor/AccessMode/writeOnly> mode:

```swift
let archiveDestination = NSTemporaryDirectory() + "directory.aar"
let archiveFilePath = FilePath(archiveDestination)

guard let writeFileStream = ArchiveByteStream.fileStream(
        path: archiveFilePath,
        mode: .writeOnly,
        options: [ .create ],
        permissions: FilePermissions(rawValue: 0o644)) else {
    return
}
defer {
    try? writeFileStream.close()
}
```

### Create the compression stream

Create the compression stream, and specify the compression algorithm as <doc://com.apple.documentation/documentation/AppleArchive/ArchiveCompression/lzfse>. Specify the file-writing stream as the stream that receives the compressed data:

```swift
guard let compressStream = ArchiveByteStream.compressionStream(
        using: .lzfse,
        writingTo: writeFileStream) else {
    return
}
defer {
    try? compressStream.close()
}
```

### Create the encoding stream

Create the encoding stream. The encoding stream encodes its data as a byte stream, and sends the encoded data to the compression stream:

```swift
guard let encodeStream = ArchiveStream.encodeStream(writingTo: compressStream) else {
    return
}
defer {
    try? encodeStream.close()
}
```

### Define the header keys

Create a field key set that defines the fields in the archive header:

```swift
guard let keySet = ArchiveHeader.FieldKeySet("TYP,PAT,LNK,DEV,DAT,UID,GID,MOD,FLG,MTM,BTM,CTM") else {
    return
}
```

For more information about three-letter keys, see <doc://com.apple.documentation/documentation/AppleArchive/ArchiveHeader/FieldKeySet/init(_:)>.

### Compress the directory contents

Use <doc://com.apple.documentation/documentation/AppleArchive/ArchiveStream/writeDirectoryContents(archiveFrom:path:keySet:selectUsing:flags:threadCount:)> to write the directory contents to the encode stream. In turn, the encode stream writes to the compression stream and then, the compression stream writes to the file stream. Finally, the file stream writes the archive file to the file system:

```swift
let sourcePath = NSTemporaryDirectory() + "src/"
let source = FilePath(sourcePath)

do {
    try encodeStream.writeDirectoryContents(
        archiveFrom: source,
        keySet: keySet)
} catch {
    fatalError("Write directory contents failed.")
}
```

On return, `directory.aar` exists in <doc://com.apple.documentation/documentation/Foundation/NSTemporaryDirectory()> and contains the compressed contents of `src/`.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)