ZIP Archive Manipulation
Relevant source files
The following files were used as context for generating this wiki page:
The ipatool project utilizes ZIP archive manipulation to manage and modify iOS application packages (.ipa files). Since an IPA file is essentially a ZIP-compressed archive containing the application bundle and metadata, the project implements logic to extract, replicate, and inject specific files—such as FairPlay DRM signatures (SINF) and iTunes metadata—into these archives.
This functionality is primarily used during the post-download phase to "patch" the downloaded raw package with user-specific account information and necessary licensing data to ensure the IPA is valid for the target device.
Sources: pkg/appstore/appstore_download.go:179-204, pkg/appstore/appstore_replicate_sinf.go:23-45
Core Archive Workflows
Archive manipulation in ipatool follows a pattern of "Read-Modify-Write." Because the archive/zip standard library in Go does not support in-place modification, the tool creates a temporary archive, replicates the original contents, and appends or overwrites specific entries.
Replicating ZIP Structures
The tool implements a replicateZip function that performs a low-level copy of all files from a source ZIP reader to a destination ZIP writer. It uses CreateRaw to preserve original compression methods and metadata for each entry.
The diagram shows the iterative process of copying compressed data between archives without decompressing.
Sources: pkg/appstore/appstore_replicate_sinf.go:138-160
IPA Patching and Metadata Injection
During the download process, the tool applies patches to the raw archive to include account-specific data. This involves two main operations: replicating the original content and adding a iTunesMetadata.plist file.
Metadata Insertion
The writeMetadata function generates a binary Property List (plist) containing the user's Apple ID and user name, then creates a new entry named iTunesMetadata.plist at the root of the ZIP archive.
| File Created | Format | Content |
|---|---|---|
iTunesMetadata.plist | Binary Plist | Apple ID, UserName, and App Metadata |
Sources: pkg/appstore/appstore_download.go:206-228
FairPlay DRM (SINF) Replication
A critical component of ZIP manipulation is the replication of .sinf files, which contain the FairPlay DRM license data. The tool supports two methods for determining where to place these files within the ZIP structure based on the app's internal configuration.
Manifest-based Replication
If the app bundle contains a SC_Info/Manifest.plist, the tool reads the SinfPaths from this plist to determine the destination paths for multiple SINF files.
Info-based Replication
If no manifest is found, the tool defaults to the standard location: Payload/[BundleName].app/SC_Info/[ExecutableName].sinf.
This sequence illustrates the logic used to decide the injection point for DRM data.
Sources: pkg/appstore/appstore_replicate_sinf.go:54-73, pkg/appstore/appstore_replicate_sinf.go:95-136
File Path Resolution within Archives
The tool parses the internal structure of the ZIP to find entry points. It specifically looks for the .app directory to resolve bundle names and executable names.
- Bundle Name Detection: Scans the ZIP for files containing
.app/Info.plist(excluding WatchKit apps) and extracts the base name. - Executable Detection: Reads the
CFBundleExecutablekey from the internalInfo.plistfile.
| Search Target | Match Pattern | Purpose |
|---|---|---|
| Info Plist | *.app/Info.plist | Identify bundle name and executable |
| Manifest | *.app/SC_Info/Manifest.plist | Identify SINF destination paths |
Sources: pkg/appstore/appstore_replicate_sinf.go:162-234
Summary of ZIP Operations
Archive manipulation is a high-integrity operation in ipatool. The process ensures that:
- The original IPA structure is preserved via raw copying.
- DRM components (SINF) are correctly mapped to their required sub-directories.
- Ownership metadata is injected to allow the IPA to be recognized by iOS devices and iTunes.
- Temporary files are used during the process (
.tmp) and swapped only upon successful completion to prevent archive corruption.
Sources: pkg/appstore/appstore_download.go:87-105, pkg/appstore/appstore_replicate_sinf.go:30-93