remotezip


Nameremotezip JSON
Version 0.12.3 PyPI version JSON
download
home_pagehttps://github.com/gtsystem/python-remotezip
SummaryAccess zip file content hosted remotely without downloading the full file.
upload_time2024-02-26 19:51:04
maintainer
docs_urlNone
authorGiuseppe Tribulato
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # remotezip

[![Build Status](https://travis-ci.org/gtsystem/python-remotezip.svg?branch=master)](https://travis-ci.org/gtsystem/python-remotezip)

This module provides a way to access single members of a zip file archive without downloading the full content from a remote web server. For this library to work, the web server hosting the archive needs to support the [range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) header. 

## Installation

`pip install remotezip`

## Usage

### Initialization

`RemoteZip(url, ...)`

To download the content, this library rely on the `requests` module. The constructor interface matches the function `requests.get` module.

* **url**: Url where the zip file is located *(required)*.
* **auth**: authentication credentials.
* **headers**: headers to pass to the request.
* **timeout**: timeout for the request.
* **verify**: enable/disable certificate verification or set custom certificates location.
* ... Please look at the [requests](http://docs.python-requests.org/en/master/user/quickstart/#make-a-request) documentation for futher usage details.
* **initial\_buffer\_size**: How much data (in bytes) to fetch during the first connection to download the zip file central directory. If your zip file conteins a lot of files, would be a good idea to increase this parameter in order to avoid the need for further remote requests. *Default: 64kb*.
* **session**: a custom session object to use for the request.
* **support_suffix_range**: You can set this attribute to `False` if the remote server doesn't support suffix range
  (negative offset). Notice that this option will use one more HEAD request to fetch the content length.

### Class Interface

`RemoteZip` is a subclass of the python standard library class `zipfile.ZipFile`, so it supports all its read methods:

* `RemoteZip.close()`
* `RemoteZip.getinfo(name)`
* `RemoteZip.extract(member[, path[, pwd]])`
* `RemoteZip.extractall([path[, members[, pwd]]])`
* `RemoteZip.infolist()`
* `RemoteZip.namelist()`
* `RemoteZip.open(name[, mode[, pwd]])`
* `RemoteZip.printdir()`
* `RemoteZip.read(name[, pwd])`
* `RemoteZip.testzip()`
* `RemoteZip.filename`
* `RemoteZip.debug`
* `RemoteZip.comment`

Please look at the [zipfile](https://docs.python.org/3/library/zipfile.html#zipfile-objects) documentation for usage details.


**NOTE**:
* `extractall()` and `testzip()` require to access the full content of the archive. If you need to use such methods, a full download of it would be probably more efficient.
* `RemoteZip.open()` now supports seek operations when reading archive members. However as the content is streamed and DEFLATE format doesn't support seek natively, any negative seek operation will result in a new remote request from the beginning of the member content. This is very inefficient, the recommandation is to use `RemoteZip.extract()` and then open and operate on the extracted file.

### Examples

#### List members in archive

Print all members part of the archive:

```python
from remotezip import RemoteZip

with RemoteZip('http://.../myfile.zip') as zip:
    for zip_info in zip.infolist():
        print(zip_info.filename)
```


#### Download a member
The following example will extract the file `somefile.txt` from the archive stored at the url `http://.../myfile.zip`.

```python
from remotezip import RemoteZip

with RemoteZip('http://.../myfile.zip') as zip:
    zip.extract('somefile.txt')
```

#### S3 example

If you are trying to download a member from a zip archive hosted on S3 you can use the [aws-requests-auth](https://github.com/DavidMuller/aws-requests-auth) library for that as follow: 

```python
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
from hashlib import sha256

auth = BotoAWSRequestsAuth(
    aws_host='s3-eu-west-1.amazonaws.com',
    aws_region='eu-west-1',
    aws_service='s3'
)
headers = {'x-amz-content-sha256': sha256('').hexdigest()}
url = "https://s3-eu-west-1.amazonaws.com/.../file.zip"

with RemoteZip(url, auth=auth, headers=headers) as z: 
    zip.extract('somefile.txt')
```

## Command line tool

A simple command line tool is included in this distribution.

```
usage: remotezip [-h] [-l] [-d DIR] url [filename [filename ...]]

Unzip remote files

positional arguments:
  url                Url of the zip archive
  filename           File to extract

optional arguments:
  -h, --help         show this help message and exit
  -l, --list         List files in the archive
  -d DIR, --dir DIR  Extract directory, default current directory
```

#### Example

```
$ remotezip -l "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip"
  Length  DateTime             Name
--------  -------------------  ------------------------
    2962  2008-07-30 13:58:46  Readme.txt
   24740  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.dbf
     145  2008-03-12 13:11:54  TM_WORLD_BORDERS-0.3.prj
 6478464  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.shp
    2068  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.shx
    
$ remotezip "http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip" Readme.txt
Extracting Readme.txt...
```

## How it works

This module uses the `zipfile.ZipFile` class under the hood to decode the zip file format. The `ZipFile` class is initialized with a file like object that will perform transparently the remote queries.

The zip format is composed by the content of each compressed member followed by the central directory.

How many requests will this module perform to download a member?

* If the full archive content is smaller than **initial\_buffer\_size**, only one request will be needed.
* Normally two requests are needed, one to download the central directory and one to download the archive member.
* If the central directory is bigger than **initial\_buffer\_size**, a third request will be required.
* If negative seek operations are used in `ZipExtFile`, each of them will result in a new request.

## Alternative modules

There is a similar module available for python [pyremotezip](https://github.com/fcvarela/pyremotezip).



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/gtsystem/python-remotezip",
    "name": "remotezip",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Giuseppe Tribulato",
    "author_email": "gtsystem@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/fa/8d/908ad46bff752568a409ee6ac797c3c6817501db06f142989e3208414569/remotezip-0.12.3.tar.gz",
    "platform": null,
    "description": "# remotezip\n\n[![Build Status](https://travis-ci.org/gtsystem/python-remotezip.svg?branch=master)](https://travis-ci.org/gtsystem/python-remotezip)\n\nThis module provides a way to access single members of a zip file archive without downloading the full content from a remote web server. For this library to work, the web server hosting the archive needs to support the [range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) header. \n\n## Installation\n\n`pip install remotezip`\n\n## Usage\n\n### Initialization\n\n`RemoteZip(url, ...)`\n\nTo download the content, this library rely on the `requests` module. The constructor interface matches the function `requests.get` module.\n\n* **url**: Url where the zip file is located *(required)*.\n* **auth**: authentication credentials.\n* **headers**: headers to pass to the request.\n* **timeout**: timeout for the request.\n* **verify**: enable/disable certificate verification or set custom certificates location.\n* ... Please look at the [requests](http://docs.python-requests.org/en/master/user/quickstart/#make-a-request) documentation for futher usage details.\n* **initial\\_buffer\\_size**: How much data (in bytes) to fetch during the first connection to download the zip file central directory. If your zip file conteins a lot of files, would be a good idea to increase this parameter in order to avoid the need for further remote requests. *Default: 64kb*.\n* **session**: a custom session object to use for the request.\n* **support_suffix_range**: You can set this attribute to `False` if the remote server doesn't support suffix range\n  (negative offset). Notice that this option will use one more HEAD request to fetch the content length.\n\n### Class Interface\n\n`RemoteZip` is a subclass of the python standard library class `zipfile.ZipFile`, so it supports all its read methods:\n\n* `RemoteZip.close()`\n* `RemoteZip.getinfo(name)`\n* `RemoteZip.extract(member[, path[, pwd]])`\n* `RemoteZip.extractall([path[, members[, pwd]]])`\n* `RemoteZip.infolist()`\n* `RemoteZip.namelist()`\n* `RemoteZip.open(name[, mode[, pwd]])`\n* `RemoteZip.printdir()`\n* `RemoteZip.read(name[, pwd])`\n* `RemoteZip.testzip()`\n* `RemoteZip.filename`\n* `RemoteZip.debug`\n* `RemoteZip.comment`\n\nPlease look at the [zipfile](https://docs.python.org/3/library/zipfile.html#zipfile-objects) documentation for usage details.\n\n\n**NOTE**:\n* `extractall()` and `testzip()` require to access the full content of the archive. If you need to use such methods, a full download of it would be probably more efficient.\n* `RemoteZip.open()` now supports seek operations when reading archive members. However as the content is streamed and DEFLATE format doesn't support seek natively, any negative seek operation will result in a new remote request from the beginning of the member content. This is very inefficient, the recommandation is to use `RemoteZip.extract()` and then open and operate on the extracted file.\n\n### Examples\n\n#### List members in archive\n\nPrint all members part of the archive:\n\n```python\nfrom remotezip import RemoteZip\n\nwith RemoteZip('http://.../myfile.zip') as zip:\n    for zip_info in zip.infolist():\n        print(zip_info.filename)\n```\n\n\n#### Download a member\nThe following example will extract the file `somefile.txt` from the archive stored at the url `http://.../myfile.zip`.\n\n```python\nfrom remotezip import RemoteZip\n\nwith RemoteZip('http://.../myfile.zip') as zip:\n    zip.extract('somefile.txt')\n```\n\n#### S3 example\n\nIf you are trying to download a member from a zip archive hosted on S3 you can use the [aws-requests-auth](https://github.com/DavidMuller/aws-requests-auth) library for that as follow: \n\n```python\nfrom aws_requests_auth.boto_utils import BotoAWSRequestsAuth\nfrom hashlib import sha256\n\nauth = BotoAWSRequestsAuth(\n    aws_host='s3-eu-west-1.amazonaws.com',\n    aws_region='eu-west-1',\n    aws_service='s3'\n)\nheaders = {'x-amz-content-sha256': sha256('').hexdigest()}\nurl = \"https://s3-eu-west-1.amazonaws.com/.../file.zip\"\n\nwith RemoteZip(url, auth=auth, headers=headers) as z: \n    zip.extract('somefile.txt')\n```\n\n## Command line tool\n\nA simple command line tool is included in this distribution.\n\n```\nusage: remotezip [-h] [-l] [-d DIR] url [filename [filename ...]]\n\nUnzip remote files\n\npositional arguments:\n  url                Url of the zip archive\n  filename           File to extract\n\noptional arguments:\n  -h, --help         show this help message and exit\n  -l, --list         List files in the archive\n  -d DIR, --dir DIR  Extract directory, default current directory\n```\n\n#### Example\n\n```\n$ remotezip -l \"http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip\"\n  Length  DateTime             Name\n--------  -------------------  ------------------------\n    2962  2008-07-30 13:58:46  Readme.txt\n   24740  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.dbf\n     145  2008-03-12 13:11:54  TM_WORLD_BORDERS-0.3.prj\n 6478464  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.shp\n    2068  2008-07-30 12:16:46  TM_WORLD_BORDERS-0.3.shx\n    \n$ remotezip \"http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip\" Readme.txt\nExtracting Readme.txt...\n```\n\n## How it works\n\nThis module uses the `zipfile.ZipFile` class under the hood to decode the zip file format. The `ZipFile` class is initialized with a file like object that will perform transparently the remote queries.\n\nThe zip format is composed by the content of each compressed member followed by the central directory.\n\nHow many requests will this module perform to download a member?\n\n* If the full archive content is smaller than **initial\\_buffer\\_size**, only one request will be needed.\n* Normally two requests are needed, one to download the central directory and one to download the archive member.\n* If the central directory is bigger than **initial\\_buffer\\_size**, a third request will be required.\n* If negative seek operations are used in `ZipExtFile`, each of them will result in a new request.\n\n## Alternative modules\n\nThere is a similar module available for python [pyremotezip](https://github.com/fcvarela/pyremotezip).\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Access zip file content hosted remotely without downloading the full file.",
    "version": "0.12.3",
    "project_urls": {
        "Homepage": "https://github.com/gtsystem/python-remotezip"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7b1822316545b712dbed0119c7d3b8683a566c7da26353e344a4188b99f12692",
                "md5": "7eff4faf02b40cf1b53e699b0cae02cf",
                "sha256": "f70a4026879439ecb0a2cf848a7c176ae5ee142bbe51ec69e3344e150b2a52de"
            },
            "downloads": -1,
            "filename": "remotezip-0.12.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7eff4faf02b40cf1b53e699b0cae02cf",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8099,
            "upload_time": "2024-02-26T19:51:02",
            "upload_time_iso_8601": "2024-02-26T19:51:02.968888Z",
            "url": "https://files.pythonhosted.org/packages/7b/18/22316545b712dbed0119c7d3b8683a566c7da26353e344a4188b99f12692/remotezip-0.12.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fa8d908ad46bff752568a409ee6ac797c3c6817501db06f142989e3208414569",
                "md5": "dbea84e79da29adb59a387ec7d2e75ba",
                "sha256": "bf1ebe2be9f07a6e1c14d0e52ecffccd7a3e808dff4f9ba523c5e84d867a3fe3"
            },
            "downloads": -1,
            "filename": "remotezip-0.12.3.tar.gz",
            "has_sig": false,
            "md5_digest": "dbea84e79da29adb59a387ec7d2e75ba",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7835,
            "upload_time": "2024-02-26T19:51:04",
            "upload_time_iso_8601": "2024-02-26T19:51:04.917269Z",
            "url": "https://files.pythonhosted.org/packages/fa/8d/908ad46bff752568a409ee6ac797c3c6817501db06f142989e3208414569/remotezip-0.12.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-26 19:51:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gtsystem",
    "github_project": "python-remotezip",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "remotezip"
}
        
Elapsed time: 0.18765s