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

# Compressing single files

Compress a single file and store the result on the file system.

## Discussion

In this article, you’ll learn how to use AppleArchive to compress a single-source file, and write the compressed data to a file.

The code below compresses a file named `myFile.pdf` using the <doc://com.apple.documentation/documentation/Compression/Algorithm/lzfse> algorithm, and stores the result in a file named `myFile.pdf.lzfse`.

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

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 sourceFilePath = FilePath(NSTemporaryDirectory() + "myFile.pdf")

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

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

You also 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 archiveFilePath = FilePath(NSTemporaryDirectory() + "myFile.pdf.lzfse")

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()
}
```

### Compress the source file

Finally, call <doc://com.apple.documentation/documentation/AppleArchive/ArchiveByteStream/process(readingFrom:writingTo:)> to send the output of the file-reading stream to the compression stream. In turn, the compression stream sends its output to the file-writing stream:

```swift
do {
    _ = try ArchiveByteStream.process(readingFrom: readFileStream,
                                      writingTo: compressStream)
} catch {
    print("Handle `ArchiveByteStream.process` failed.")
}
```

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

---

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)