<!--
{
  "documentType" : "article",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/decompressing-and-extracting-an-archived-directory",
  "metadataVersion" : "0.1.0",
  "role" : "article",
  "title" : "Decompressing and extracting an archived directory"
}
-->

# Decompressing and extracting an archived directory

Recreate an entire file system directory from an archive file.

## Discussion

In this article, you’ll learn how to use AppleArchive to decompress and extract a previously compressed file system directory.

The code below decompresses the file generated using the steps explained in <doc://com.apple.documentation/documentation/Accelerate/compressing-file-system-directories> and writes the files to a directory named `dest`.

### Create the file stream to read the source archive

The <doc://com.apple.documentation/documentation/AppleArchive/ArchiveByteStream> class provides static factory methods that create streams for different functions. In this case, use <doc://com.apple.documentation/documentation/AppleArchive/ArchiveByteStream/fileStream(path:mode:options:permissions:)> to create a byte stream that reads the source file:

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

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

### Create the decompression stream

Create the decompression stream. Specify the file-reading stream as the input stream that provides the compressed data:

```swift
guard let decompressStream = ArchiveByteStream.decompressionStream(readingFrom: readFileStream) else {
    return
}
defer {
    try? decompressStream.close()
}
```

### Create the decoding stream

Create a decoding stream that provides archive elements from the raw, decompressed data:

```swift
guard let decodeStream = ArchiveStream.decodeStream(readingFrom: decompressStream) else {
    print("unable to create decode stream")
    return
}
defer {
    try? decodeStream.close()
}
```

### Specify the destination

Specify a destination path for the decompressed directory contents. The following code checks that the destination directory exists and creates the destination if neccessary:

```swift
let decompressPath = NSTemporaryDirectory() + "dest/"

if !FileManager.default.fileExists(atPath: decompressPath) {
    do {
        try FileManager.default.createDirectory(atPath: decompressPath,
                                                withIntermediateDirectories: false)
    } catch {
        fatalError("Unable to create destination directory.")
    }
}

let decompressDestination = FilePath(decompressPath)
```

### Create the extract stream

Create an extract stream that receives archive elements, and extracts to the specified directory:

```swift
guard let extractStream = ArchiveStream.extractStream(extractingTo: decompressDestination,
                                                      flags: [ .ignoreOperationNotPermitted ]) else {
    return
}
defer {
    try? extractStream.close()
}
```

### Decompress and extract the archived directory

Finally, call <doc://com.apple.documentation/documentation/AppleArchive/ArchiveByteStream/process(readingFrom:writingTo:)> to write the output of the decode stream to the extract stream. In turn, the extract stream extracts each archive element to the decompression destination:

```swift
do {
    _ = try ArchiveStream.process(readingFrom: decodeStream,
                                  writingTo: extractStream)
} catch {
    print("process failed")
}
```

On return, the operation recreates the contents of the directory previously archived in `directory.aar` in <doc://com.apple.documentation/documentation/Foundation/NSTemporaryDirectory()> `+ “dest/”.`

---

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)