Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

Làm cách nào để mở các tệp trong kho lưu trữ zip mà không trích xuất chúng trước?

Tôi đang sử dụng pygame. Để lưu không gian đĩa, tôi có tất cả các hình ảnh được nén lại. Có thể tải một hình ảnh đã cho trực tiếp từ tệp zip không? Ví dụ:

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
2

Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

d-cubed

9945 Huy hiệu vàng30 Huy hiệu bạc56 Huy hiệu Đồng5 gold badges30 silver badges56 bronze badges

Hỏi ngày 15 tháng 10 năm 2013 lúc 1:23Oct 15, 2013 at 1:23

1

Câu trả lời của Vincent Povirk sẽ không hoàn toàn hoạt động;

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgfile = archive.open('img_01.png')
...

Bạn phải thay đổi nó trong:

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...

Để biết chi tiết, hãy đọc các tài liệu

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
3 ở đây.

Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

Phượng Hoàng

6.8584 Huy hiệu vàng37 Huy hiệu bạc44 Huy hiệu đồng4 gold badges37 silver badges44 bronze badges

Đã trả lời ngày 15 tháng 10 năm 2013 lúc 1:52Oct 15, 2013 at 1:52

JellemajellemaJellema

1.7521 huy hiệu vàng11 Huy hiệu bạc15 Huy hiệu đồng1 gold badge11 silver badges15 bronze badges

3

import io, pygame, zipfile
archive = zipfile.ZipFile('images.zip', 'r')

# read bytes from archive
img_data = archive.read('img_01.png')

# create a pygame-compatible file-like object from the bytes
bytes_io = io.BytesIO(img_data)

img = pygame.image.load(bytes_io)

Tôi đã cố gắng tìm ra điều này cho chính mình và nghĩ rằng điều này có thể hữu ích cho bất kỳ ai bắt gặp câu hỏi này trong tương lai.

Đã trả lời ngày 27 tháng 1 năm 2014 lúc 17:26Jan 27, 2014 at 17:26

BrandonbrandonBrandon

3213 Huy hiệu bạc2 Huy hiệu đồng3 silver badges2 bronze badges

Về lý thuyết, vâng, đó chỉ là vấn đề cắm mọi thứ vào. Zipfile có thể cung cấp cho bạn một đối tượng giống như tệp cho một tệp trong kho lưu trữ zip và Image.load sẽ chấp nhận một đối tượng giống như tệp. Vì vậy, một cái gì đó như thế này nên làm việc:

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgfile = archive.open('img_01.png')
try:
    image = pygame.image.load(imgfile, 'img_01.png')
finally:
    imgfile.close()

Đã trả lời ngày 15 tháng 10 năm 2013 lúc 1:36Oct 15, 2013 at 1:36

Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

Esme Povirkesme PovirkEsme Povirk

2.91014 Huy hiệu bạc24 Huy hiệu đồng14 silver badges24 bronze badges

Từ Python 3.2 trở đi, có thể sử dụng

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
3 làm trình quản lý bối cảnh:

from zipfile import ZipFile

with ZipFile('images.zip') as zf:
    for file in zf.namelist():
        if not file.endswith('.png'): # optional filtering by filetype
            continue
        with zf.open(file) as f:
            image = pygame.image.load(f, namehint=file)

  • Mặt tích cực của việc sử dụng các trình quản lý ngữ cảnh (câu lệnh
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    5) là các tệp được tự động đóng đúng.
  • import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    6 có thể được sử dụng như đối tượng tệp thông thường bạn sẽ nhận được khi sử dụng Open in () tích hợp.

Liên kết đến tài liệu

  • Zipfile, zipfile.namelist, zipfile.open
  • PEP 343 (với tuyên bố)

Đã trả lời ngày 14 tháng 1 lúc 17:14Jan 14 at 17:14

Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

np8np8np8

23.8K10 Huy hiệu vàng82 Huy hiệu bạc90 Huy hiệu Đồng10 gold badges82 silver badges90 bronze badges

Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén

  1. Làm thế nào để
  2. Python làm thế nào
  3. Mở tệp zip mà không trích xuất nó trong

  1. Sử dụng chức năng
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    7 để mở tệp zip mà không tạm thời trích xuất nó trong Python
  2. Sử dụng chức năng
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    8 để mở tệp zip mà không tạm thời trích xuất nó trong Python

Bài viết này giải thích cách mở tệp zip mà không cần trích xuất tạm thời nó trong phần mềm Python. Để mở một tệp zip mà không cần trích xuất nó trong Python, hãy sử dụng thư viện python

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
9.

Đối với điều này, nhập thư viện tiêu chuẩn

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
9. Sau đó, sử dụng một trong hai chức năng sau.

  • Sử dụng hàm
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    7 trong chế độ đọc.
  • Sử dụng hàm
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgdata = archive.read('img_01.png')
    ...
    
    8 trong chế độ đọc.

Trước khi chúng tôi bắt đầu, xin vui lòng chuẩn bị sẵn tệp zip. Thực hiện theo các bước dưới đây.

  • Chuẩn bị một tệp văn bản có tên
    import io, pygame, zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    
    # read bytes from archive
    img_data = archive.read('img_01.png')
    
    # create a pygame-compatible file-like object from the bytes
    bytes_io = io.BytesIO(img_data)
    
    img = pygame.image.load(bytes_io)
    
    3 với một số nội dung bên trong đó là:
    This is from mail.txt
    
  • Zip tệp
    import io, pygame, zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    
    # read bytes from archive
    img_data = archive.read('img_01.png')
    
    # create a pygame-compatible file-like object from the bytes
    bytes_io = io.BytesIO(img_data)
    
    img = pygame.image.load(bytes_io)
    
    3.
  • Đặt tên cho tệp zip là
    import io, pygame, zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    
    # read bytes from archive
    img_data = archive.read('img_01.png')
    
    # create a pygame-compatible file-like object from the bytes
    bytes_io = io.BytesIO(img_data)
    
    img = pygame.image.load(bytes_io)
    
    5.

Dưới đây là một chương trình ví dụ chỉ cho bạn cách mở tệp zip mà không tạm thời trích xuất nó trong Python. Sử dụng hàm

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
7 trong chế độ đọc theo cách sau.

zipfile.ZipFile(file, mode='r')

Ở đây,

import io, pygame, zipfile
archive = zipfile.ZipFile('images.zip', 'r')

# read bytes from archive
img_data = archive.read('img_01.png')

# create a pygame-compatible file-like object from the bytes
bytes_io = io.BytesIO(img_data)

img = pygame.image.load(bytes_io)
7 là:

  • Một đường dẫn đến một tệp (một chuỗi)
  • Một đối tượng giống như tệp
  • Một đối tượng giống như đường dẫn

Ví dụ,

import zipfile
archive = zipfile.ZipFile('mail.zip', 'r')
#Let us verify the operation..
txtdata = archive.read('mail.txt')
print(txtdata)

Output:

b'This is from mail.txt'

Ở đây, một ví dụ trình bày cách mở tệp zip mà không cần trích xuất tạm thời nó trong Python.

Ở đây, chúng tôi sử dụng hàm

import io, pygame, zipfile
archive = zipfile.ZipFile('images.zip', 'r')

# read bytes from archive
img_data = archive.read('img_01.png')

# create a pygame-compatible file-like object from the bytes
bytes_io = io.BytesIO(img_data)

img = pygame.image.load(bytes_io)
8 trong chế độ đọc.

ZipFile.open(name, mode='r')

Thành viên của tệp zip được coi là đối tượng giống như tệp nhị phân.

import io, pygame, zipfile
archive = zipfile.ZipFile('images.zip', 'r')

# read bytes from archive
img_data = archive.read('img_01.png')

# create a pygame-compatible file-like object from the bytes
bytes_io = io.BytesIO(img_data)

img = pygame.image.load(bytes_io)
9 ở đây có thể là:

  • Tên của một tệp trong zip
  • Một đối tượng
    import zipfile
    archive = zipfile.ZipFile('images.zip', 'r')
    imgfile = archive.open('img_01.png')
    try:
        image = pygame.image.load(imgfile, 'img_01.png')
    finally:
        imgfile.close()
    
    0

Đây là một ví dụ.

import zipfile
archive = zipfile.ZipFile('images.zip', 'r')
imgdata = archive.read('img_01.png')
...
0

Output:

b'This is from mail.txt'

Bài viết liên quan - Tệp Python

  • Nhận tất cả các tệp của một thư mục
  • Xóa một tệp và thư mục trong Python
  • Nối văn bản vào một tệp trong Python
  • Kiểm tra xem một tệp có tồn tại trong Python không
    Hướng dẫn python read zip file without extracting - python đọc tệp zip mà không cần giải nén
  • Python có thể đọc một tệp zip không?

    Python có thể làm việc trực tiếp với dữ liệu trong các tệp zip.Bạn có thể xem danh sách các mục trong thư mục và làm việc với các tệp dữ liệu.. You can look at the list of items in the directory and work with the data files themselves.

    Tệp zip trong Python là gì?

    Zipfile của Python là một mô -đun thư viện tiêu chuẩn nhằm thao tác các tệp zip.Định dạng tệp này là một tiêu chuẩn công nghiệp được áp dụng rộng rãi khi nói đến việc lưu trữ và nén dữ liệu kỹ thuật số.Bạn có thể sử dụng nó để đóng gói một số tệp liên quan.a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.