添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

工作中需要将大批的数据,压缩为zip存储。按照传统的处理办法需要将数据先存储到本地磁盘,再从磁盘读文件压缩成zip文件。
传统方法需要多次磁盘IO,性能很低,如果跳过文件存储,直接将内存的数据压缩保存,会大大减少磁盘IO,提升性能。

不需要看解析的,可以直接看最后完整的python代码

创建一个类: InMemoryZIP(), 来处理所有的程序。

class InMemoryZIP(object):

init() 创建一个类文件对象

	def __init__(self):
		# create the in-memory file-like object
		self.in_memory_zip = BytesIO()

二 append() 内存数据添加到zip对象

	def append(self, filename_in_zip, file_contents):
		""" Appends a file with name filename_in_zip \
        and contents of file_contents to the in-memory zip.
        # create a handle to the in-memory zip in append mode\
		if not isinstance(file_contents, bytes):
			file_contens = bytes(str(file_contens), encoding='utf-8')
		# write the file to the in-memory zip
		zf = zipfile.ZipFile(self.in_memory_zip, 'a', zipfile.ZIP_DEFLATED, False)
		zf.writestr(filename_in_zip, file_contents)
		# mark the files as having been created on Windows
        # so that Unix permissions are not inferred as 0000
		for zfiel in zf.filelist:
			zfile.create_system = 0
		return self

三 appendfile() 文件添加到zip对象

	def appendfile(self, file_path, file_name=None):
		""" Read a file with path file_path \
        and append to in-memory zip with name file_name.
        # file_path is abs path
		if file_name is None:
			file_name = os.path.split(file_path)[1]
		with open(file_path, 'rb') as f:
			file_contents = f.read()
			self.append(file_name, file_contents)
		return self

四 read() 读取zip数据流

	def read(self):
		""" Returns a string with the contents of the in-memory zip.
		self.in_memory_zip.seek(0)
		return self.in_memory_zip.read()

五 writetofile() 内存zip流保存为zip文件

def writetofile(self, zip_filename):
        Write the in-memory zip to a file
	with open(zip_filename, 'wb') as f:
		f.write(self.read())

六 完整版python代码

# !user/bin/env python3
# -*-coding : utf-8 -*-
import zipfile
from io import BytesIO
import os
class InMemoryZIP(object):
    def __init__(self):
        # create the in-memory file-like object
        self.in_memory_zip = BytesIO()
    def append(self, filename_in_zip, file_contents):
        """ Appends a file with name filename_in_zip \
        and contents of file_contents to the in-memory zip.
        # create a handle to the in-memory zip in append mode\
        if not isinstance(file_contents, bytes):
            file_contents = bytes(str(file_contents), encoding='utf-8')
        zf = zipfile.ZipFile(self.in_memory_zip, 'a',
                             zipfile.ZIP_DEFLATED, False)
        # write the file to the in-memory zip
        zf.writestr(filename_in_zip, file_contents)
        # mark the files as having been created on Windows
        # so that Unix permissions are not inferred as 0000
        for zfile in zf.filelist:
            zfile.create_system = 0
        return self
    def appendfile(self, file_path, file_name=None):
        """ Read a file with path file_path \
        and append to in-memory zip with name file_name.
        if file_name is None:
            file_name = os.path.split(file_path)[1]
        f = open(file_path, 'rb')
        file_contents = f.read()
        self.append(file_name, file_contents)
        f.close()
        return self
    def read(self):
        """ Returns a string with the contents of the in-memory zip.
        self.in_memory_zip.seek(0)
        return self.in_memory_zip.read()
    def writetofile(self, filename):
        Write the in-memory zip to a file
        f = open(filename, 'wb')
        f.write(self.read())
        f.close()
if __name__ == '__main__':
	# contens = 'xxxxxxxxx'  # 内存数据
    # imz = InMemoryZIP()
    # imz.append('test.txt', contens)
    # imz.writetofile('test.zip')
                    文章目录一 __init__() 创建一个类文件对象二 append() 内存数据添加到zip对象三 appendfile() 文件添加到zip对象四 read() 读取zip数据流五 writetofile() 内存zip流保存为zip文件六 完整版python代码工作中需要将大批的数据,压缩为zip存储。按照传统的处理办法需要将数据先存储到本地磁盘,再从磁盘读文件压缩成zip文件。传统方法需要多次磁盘IO,性能很低,如果跳过文件存储,直接将内存的数据压缩保存,会大大减少磁盘IO,提升性能。不
				
进入到21世纪之后,最痛苦的事情,就是数据大爆炸。因为生活无处不在生成数据,比如你发一下微信,发一下图片,再照一些图片,再扫码付款,再到乘车等等。现在各个大公司都在处理大数据的问题,有人说数据就是石油,因为有了数据才能训练AI的模型,才能把数据当燃料,不断地驱动AI向前发展。 当你现在走进沃尔玛时,再也看不到以前的秤,取而代之的是人工智能电子秤。这个人工智能的电子秤非常好用,与之前相比有很大的进步。在这之前,当人们要去买苹果时,拿袋子去选择了苹果之后,拿到秤那里进行称重,这是比较麻烦的事情。因为放...
本代码采用MIT许可 使用io.BytesIO()在内存压缩,解压缩zip文件 2014-04-30 yaoyu 创建 import zipfile with zipfile.ZipFile('spam.zip', 'w') as myzip: myzip.write('eggs.txt') myzip.write('beef.txt') 这样就生成了一个包含两个文件压缩包 spam.zip, 相当于命令zip spam.zip egges.txt beef.txt的效果。用unzip -l spam.z...
Python文件进行压缩,可以使用 zipfile库,提供了非常丰富的API。 zipfile本身是上下文管理器, 可以使用with。以下为自己简单写的Demo。 import os import zipfile def file2zip(zip_file_name: str, file_names: list): """ 将多个文件夹中文件压缩存储为zip :param zip_file_name: /root/Document/test.zip :para
引用「mkdir /home/roo/swapfile」 大佬,在执行创建文件的时候一直失败提示 mkdir: 无法创建目录"/home/roo/swapfile": 没有那个文件或目录 可能是因为什么原因啊
pandas 读取excel文件 littleRpl: 中文可以的,报错一般是字符编码的问题。你可以分两步来。第一步,创建一个文件对象, f = open('XXX', 'rb'), 第二步,df = pd.read_execel(f, ). pandas 读取excel文件 遇事别慌张先生: 为什么我读取文件总是失败