Python zip có phải là trình tạo không?

zipstream. py là trình tạo kho lưu trữ zip dựa trên python 3. tệp zip của 3. py. Nó được tạo để tạo trình tạo tệp zip để phát trực tuyến [tức là ứng dụng web]. Điều này có lợi khi bạn muốn cung cấp kho lưu trữ có thể tải xuống của một bộ sưu tập lớn các tệp thông thường, điều này sẽ không khả thi để tạo kho lưu trữ trước khi tải xuống hoặc của một tệp rất lớn mà bạn không muốn lưu trữ hoàn toàn trên đĩa hoặc trên bộ nhớ

Kho lưu trữ được tạo dưới dạng trình lặp chuỗi, khi được nối, tạo thành kho lưu trữ zip. Ví dụ: đoạn mã sau sẽ viết một kho lưu trữ zip chứa các tệp từ 'đường dẫn' đến một tệp bình thường

import zipstream

z = zipstream.ZipFile[]
z.write['path/to/files']

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]

zipstream cũng cho phép lấy một chuỗi byte có thể lặp lại làm đầu vào và tạo kho lưu trữ dưới dạng một trình lặp. Điều này tránh lưu trữ các tệp lớn trên đĩa hoặc trong bộ nhớ. Để làm như vậy, bạn có thể sử dụng một cái gì đó như đoạn trích này

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]

Tất nhiên cả hai cách tiếp cận có thể được kết hợp

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write['path/to/files', 'my_archive_files']
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]

Vì các phiên bản web gần đây. py hỗ trợ trả về các trình vòng lặp của chuỗi sẽ được gửi tới trình duyệt, để tải xuống kho lưu trữ được tạo động, bạn có thể sử dụng một cái gì đó như đoạn mã này

Trình tạo tệp zip có thể phát trực tuyến hiện đại và dễ sử dụng. Nó có thể đóng gói và truyền trực tuyến nhiều tệp và thư mục mà không cần các tệp tạm thời hoặc bộ nhớ quá mức

Bao gồm khả năng tính tổng kích thước của luồng trước khi bất kỳ dữ liệu nào thực sự được thêm vào [miễn là không sử dụng nén]. Điều này làm cho nó trở nên lý tưởng để sử dụng trong các ứng dụng web vì tổng kích thước có thể được sử dụng để đặt tiêu đề

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write['path/to/files', 'my_archive_files']
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
2 mà không phải tạo toàn bộ tệp trước [xem ví dụ bên dưới]

Đặc trưng

  • Tạo dữ liệu zip một cách nhanh chóng khi được yêu cầu
  • Có thể tính toán tổng kích thước của tệp zip kết quả trước khi bắt đầu tạo
  • API linh hoạt. Các trường hợp sử dụng điển hình là đơn giản, phức tạp là có thể
  • Hỗ trợ nén dữ liệu từ tệp, byte, chuỗi và bất kỳ đối tượng có thể lặp lại nào khác
  • chủ đề an toàn. Sẽ không xáo trộn dữ liệu nếu nhiều luồng đồng thời thêm/đọc dữ liệu vào/từ cùng một luồng
  • Bao gồm một bản sao mô-đun
    def iterable[]:
        for _ in xrange[10]:
            yield b'this is a byte string\x01\n'
    
    z = zipstream.ZipFile[]
    z.write['path/to/files', 'my_archive_files']
    z.write_iter['my_archive_iter', iterable[]]
    
    with open['zipfile.zip', 'wb'] as f:
        for data in z:
            f.write[data]
    
    3 của Python với hỗ trợ zip được thêm vào. Hãy thử
    def iterable[]:
        for _ in xrange[10]:
            yield b'this is a byte string\x01\n'
    
    z = zipstream.ZipFile[]
    z.write['path/to/files', 'my_archive_files']
    z.write_iter['my_archive_iter', iterable[]]
    
    with open['zipfile.zip', 'wb'] as f:
        for data in z:
            f.write[data]
    
    4
  • Tự động sử dụng các tiện ích mở rộng Zip64, nhưng chỉ khi chúng được yêu cầu
  • Không phụ thuộc bên ngoài

Cài đặt

pip install zipstream-ng

ví dụ

Tạo một tệp zip cục bộ [ví dụ đơn giản]

Tạo một kho lưu trữ có tên là

from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
0 trong thư mục hiện tại chứa tất cả các tệp trong
from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
1

from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]

Tạo tệp zip cục bộ [trình diễn thêm về API]

from zipstream import ZipStream, ZIP_DEFLATED

# Create a ZipStream that uses the maximum level of Deflate compression.
zs = ZipStream[compress_type=ZIP_DEFLATED, compress_level=9]

# Set the zip file's comment.
zs.comment = "Contains compressed important files"

# Add all the files under a path.
# Will add all files under a top-level folder called "files" in the zip.
zs.add_path["/path/to/files/"]

# Add another file [will be added as "data.txt" in the zip file].
zs.add_path["/path/to/file.txt", "data.txt"]

# Add some random data from an iterable.
# This generator will only be run when the stream is generated.
def random_data[]:
    import random
    for _ in range[10]:
        yield random.randbytes[1024]

zs.add[random_data[], "random.bin"]

# Add a file containing some static text.
# Will automatically be encoded to bytes before being added [uses utf-8].
zs.add["This is some text", "README.txt"]

# Write out the zip file as it's being generated.
# At this point the data in the files files will be read in and the generator
# will be iterated over.
with open["files.zip", "wb"] as f:
    f.writelines[zs]

máy chủ zip [bao gồm]

Có thể tìm thấy một ví dụ hữu ích và đầy đủ chức năng trong mô-đun

from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
2 đi kèm. Đó là một bản sao của Python được xây dựng trong
def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write['path/to/files', 'my_archive_files']
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
3 với khả năng bổ sung để phân phát nhiều tệp và thư mục dưới dạng một tệp zip. Hãy dùng thử bằng cách cài đặt gói và chạy
from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
4 hoặc
from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
5

Tích hợp với ứng dụng web Flask

Một máy chủ tệp dựa trên Flask rất cơ bản truyền tất cả các tệp theo đường dẫn được yêu cầu tới máy khách dưới dạng tệp zip. Nó cung cấp tổng kích thước của luồng trong tiêu đề

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write['path/to/files', 'my_archive_files']
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
2 để khách hàng có thể hiển thị thanh tiến trình khi luồng được tải xuống. Nó cũng cung cấp một tiêu đề
from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
7 để khách hàng có thể kiểm tra xem nó đã có bản sao dữ liệu được nén gần đây nhất hay chưa với yêu cầu
from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
8 thay vì phải tải xuống tệp và kiểm tra

Lưu ý rằng trong khi ví dụ này hoạt động, bạn không nên triển khai nguyên trạng do thiếu xác thực đầu vào và các kiểm tra khác

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
3

Tạo một phần và bổ sung tệp vào phút cuối

Có thể tạo luồng zip, nhưng hãy dừng lại trước khi hoàn thiện luồng đó. Điều này cho phép thêm một cái gì đó như tệp kê khai hoặc nhật ký nén sau khi tất cả các tệp đã được thêm vào

from zipstream import ZipStream

zs = ZipStream.from_path["/path/to/files/"]

with open["files.zip", "wb"] as f:
    f.writelines[zs]
9 cung cấp phương thức
from zipstream import ZipStream, ZIP_DEFLATED

# Create a ZipStream that uses the maximum level of Deflate compression.
zs = ZipStream[compress_type=ZIP_DEFLATED, compress_level=9]

# Set the zip file's comment.
zs.comment = "Contains compressed important files"

# Add all the files under a path.
# Will add all files under a top-level folder called "files" in the zip.
zs.add_path["/path/to/files/"]

# Add another file [will be added as "data.txt" in the zip file].
zs.add_path["/path/to/file.txt", "data.txt"]

# Add some random data from an iterable.
# This generator will only be run when the stream is generated.
def random_data[]:
    import random
    for _ in range[10]:
        yield random.randbytes[1024]

zs.add[random_data[], "random.bin"]

# Add a file containing some static text.
# Will automatically be encoded to bytes before being added [uses utf-8].
zs.add["This is some text", "README.txt"]

# Write out the zip file as it's being generated.
# At this point the data in the files files will be read in and the generator
# will be iterated over.
with open["files.zip", "wb"] as f:
    f.writelines[zs]
0 trả về thông tin về tất cả các tệp đã được thêm vào luồng. Trong ví dụ này, tất cả thông tin đó sẽ được thêm vào zip trong tệp có tên "manifest. json" trước khi hoàn thiện nó

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
6

So sánh với stdlib

Kể từ Python 3. 6, thực sự có thể tạo các tệp zip dưới dạng luồng chỉ bằng thư viện chuẩn, nó không tiện dụng hoặc hiệu quả lắm. Xem xét trường hợp sử dụng điển hình của việc nén một thư mục tệp trong khi truyền trực tuyến qua kết nối mạng

[lưu ý rằng kích thước của luồng không được tính toán trước trong trường hợp này vì điều này sẽ làm cho ví dụ về stdlib trở nên quá dài]

Sử dụng ZipStream

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
7

Chức năng tương tự [ish] chỉ sử dụng stdlib

def iterable[]:
    for _ in xrange[10]:
        yield b'this is a byte string\x01\n'

z = zipstream.ZipFile[]
z.write_iter['my_archive_iter', iterable[]]

with open['zipfile.zip', 'wb'] as f:
    for data in z:
        f.write[data]
8

bài kiểm tra

Gói này chứa các bài kiểm tra mở rộng. Để chạy chúng, hãy cài đặt

from zipstream import ZipStream, ZIP_DEFLATED

# Create a ZipStream that uses the maximum level of Deflate compression.
zs = ZipStream[compress_type=ZIP_DEFLATED, compress_level=9]

# Set the zip file's comment.
zs.comment = "Contains compressed important files"

# Add all the files under a path.
# Will add all files under a top-level folder called "files" in the zip.
zs.add_path["/path/to/files/"]

# Add another file [will be added as "data.txt" in the zip file].
zs.add_path["/path/to/file.txt", "data.txt"]

# Add some random data from an iterable.
# This generator will only be run when the stream is generated.
def random_data[]:
    import random
    for _ in range[10]:
        yield random.randbytes[1024]

zs.add[random_data[], "random.bin"]

# Add a file containing some static text.
# Will automatically be encoded to bytes before being added [uses utf-8].
zs.add["This is some text", "README.txt"]

# Write out the zip file as it's being generated.
# At this point the data in the files files will be read in and the generator
# will be iterated over.
with open["files.zip", "wb"] as f:
    f.writelines[zs]
1 [
from zipstream import ZipStream, ZIP_DEFLATED

# Create a ZipStream that uses the maximum level of Deflate compression.
zs = ZipStream[compress_type=ZIP_DEFLATED, compress_level=9]

# Set the zip file's comment.
zs.comment = "Contains compressed important files"

# Add all the files under a path.
# Will add all files under a top-level folder called "files" in the zip.
zs.add_path["/path/to/files/"]

# Add another file [will be added as "data.txt" in the zip file].
zs.add_path["/path/to/file.txt", "data.txt"]

# Add some random data from an iterable.
# This generator will only be run when the stream is generated.
def random_data[]:
    import random
    for _ in range[10]:
        yield random.randbytes[1024]

zs.add[random_data[], "random.bin"]

# Add a file containing some static text.
# Will automatically be encoded to bytes before being added [uses utf-8].
zs.add["This is some text", "README.txt"]

# Write out the zip file as it's being generated.
# At this point the data in the files files will be read in and the generator
# will be iterated over.
with open["files.zip", "wb"] as f:
    f.writelines[zs]
2] và chạy
from zipstream import ZipStream, ZIP_DEFLATED

# Create a ZipStream that uses the maximum level of Deflate compression.
zs = ZipStream[compress_type=ZIP_DEFLATED, compress_level=9]

# Set the zip file's comment.
zs.comment = "Contains compressed important files"

# Add all the files under a path.
# Will add all files under a top-level folder called "files" in the zip.
zs.add_path["/path/to/files/"]

# Add another file [will be added as "data.txt" in the zip file].
zs.add_path["/path/to/file.txt", "data.txt"]

# Add some random data from an iterable.
# This generator will only be run when the stream is generated.
def random_data[]:
    import random
    for _ in range[10]:
        yield random.randbytes[1024]

zs.add[random_data[], "random.bin"]

# Add a file containing some static text.
# Will automatically be encoded to bytes before being added [uses utf-8].
zs.add["This is some text", "README.txt"]

# Write out the zip file as it's being generated.
# At this point the data in the files files will be read in and the generator
# will be iterated over.
with open["files.zip", "wb"] as f:
    f.writelines[zs]
3 trong thư mục dự án

Python zip[] là gì?

Hàm zip[] của Python . returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

Trình tạo trong Python là gì?

Trong Python, trình tạo là hàm trả về một trình vòng lặp tạo ra một chuỗi giá trị khi được lặp lại . Trình tạo rất hữu ích khi chúng tôi muốn tạo một chuỗi giá trị lớn, nhưng chúng tôi không muốn lưu trữ tất cả chúng trong bộ nhớ cùng một lúc.

Python có mở máy phát điện không?

việc sử dụng phương thức open[] để mở tệp EEG sẽ tạo một đối tượng tệp có chức năng như một trình tạo mỗi lần tạo ra một dòng dữ liệu dưới dạng chuỗi.

zip được xây dựng

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về hàm zip[] trong Python với sự trợ giúp của các ví dụ. Hàm zip[] lấy các lần lặp [có thể bằng 0 hoặc nhiều hơn], tổng hợp chúng thành một bộ và trả về nó. . zip[] Tham số

Chủ Đề