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
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.
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
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
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.import zipfile archive = zipfile.ZipFile['images.zip', 'r'] imgdata = archive.read['img_01.png'] ...
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
np8np8np8
23.8K10 Huy hiệu vàng82 Huy hiệu bạc90 Huy hiệu Đồng10 gold badges82 silver badges90 bronze badges
- Làm thế nào để
- Python làm thế nào
- Mở tệp zip mà không trích xuất nó trong
- Sử dụng chức năng
7 để mở tệp zip mà không tạm thời trích xuất nó trong Pythonimport zipfile archive = zipfile.ZipFile['images.zip', 'r'] imgdata = archive.read['img_01.png'] ...
- Sử dụng chức năng
8 để mở tệp zip mà không tạm thời trích xuất nó trong Pythonimport zipfile archive = zipfile.ZipFile['images.zip', 'r'] imgdata = archive.read['img_01.png'] ...
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
7 trong chế độ đọc.import zipfile archive = zipfile.ZipFile['images.zip', 'r'] imgdata = archive.read['img_01.png'] ...
- Sử dụng hàm
8 trong chế độ đọc.import zipfile archive = zipfile.ZipFile['images.zip', 'r'] imgdata = archive.read['img_01.png'] ...
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
3 với một số nội dung bên trong đó 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]
This is from mail.txt
- Zip tệp
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 tên cho tệp zip là
5.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]
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
0import 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[]
Đây là một ví dụ.
import zipfile
archive = zipfile.ZipFile['images.zip', 'r']
imgdata = archive.read['img_01.png']
...
0Output:
b'This is from mail.txt'