Skip to main content

Python library to read and write Silkroad Online PK2 files

Project description

PK2API

Based on: https://github.com/JellyBitz/JMX-File-Editor

Python library to read and write data into the PK2 file format from Silkroad Online.

Features

  • Fast search O(1)
  • Create new PK2 files
  • Create new paths recursively if does not exist
  • Full read/write support

Installation

pip install pk2api

Or install from source:

pip install -e .

Usage

from pk2api import Pk2Stream

key = "169841"  # Default SRO key

# Read-only mode
with Pk2Stream("/path/to/Media.pk2", key, read_only=True) as pk2:
    # Get file content
    file = pk2.get_file("Type.txt")
    if file:
        content = file.get_content()
        print(content.decode("utf-8"))

    # List files from root folder
    root = pk2.get_folder("")
    print("Files:")
    for path in root.files.keys():
        print(f" - {path}")

    # List folders from root folder
    print("Folders:")
    for path in root.folders.keys():
        print(f" - {path}")

# Read-write mode
with Pk2Stream("/path/to/Media.pk2", key) as pk2:
    # Add & remove folder
    pk2.add_folder("test/new_folder")
    pk2.remove_folder("test/new_folder")

    # Add & remove file
    pk2.add_file("test/new_file.txt", b"Hello World")
    pk2.remove_file("test/new_file.txt")

API Reference

Pk2Stream

Main class for reading and writing PK2 archives.

Pk2Stream(path: str, key: str, read_only: bool = False)

Methods:

  • get_folder(path: str) -> Pk2Folder | None - Get folder by path
  • get_file(path: str) -> Pk2File | None - Get file by path
  • add_folder(path: str) -> bool - Create folder (recursive)
  • add_file(path: str, data: bytes) -> bool - Add or update file
  • remove_folder(path: str) -> bool - Remove folder and contents
  • remove_file(path: str) -> bool - Remove file
  • close() - Close the stream

Pk2Folder

Represents a folder within a PK2 archive.

Properties:

  • name: str - Folder name
  • parent: Pk2Folder | None - Parent folder
  • offset: int - Byte offset in stream
  • files: dict[str, Pk2File] - Files in this folder
  • folders: dict[str, Pk2Folder] - Subfolders

Methods:

  • get_full_path() -> str - Get full path from root

Pk2File

Represents a file within a PK2 archive.

Properties:

  • name: str - File name
  • parent: Pk2Folder - Parent folder
  • offset: int - Byte offset in stream
  • size: int - File size in bytes

Methods:

  • get_full_path() -> str - Get full path from root
  • get_content() -> bytes - Read file content

Known Issues

  • New folder/file names are saved in lowercase

Requirements

  • Python 3.10+

Special Thanks!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pk2api-1.1.0.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pk2api-1.1.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file pk2api-1.1.0.tar.gz.

File metadata

  • Download URL: pk2api-1.1.0.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pk2api-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0b2fe66cac263da792a95858f3df062881a2d79ea4f0d5462a5a69d3ce385b36
MD5 105e14588f619f39c63cc02fb996d3e7
BLAKE2b-256 4740bbd8d927c70d50d84ee8f7ead70b6cb09a8c4307b2be4c4fefde6777b32b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pk2api-1.1.0.tar.gz:

Publisher: publish.yml on dadav/pk2api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pk2api-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pk2api-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pk2api-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ccfca7521c227ece39906bbb2c55818faa714ef5ea7d0d0ad9762493d8f14b34
MD5 6124dce78991d7ffda3a5320af43a33c
BLAKE2b-256 f02a043a7b1c36c6c6bae013e4a1fcfa67755808d1bd1be13a537c514500af34

See more details on using hashes here.

Provenance

The following attestation bundles were made for pk2api-1.1.0-py3-none-any.whl:

Publisher: publish.yml on dadav/pk2api

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page