Search code examples
pythonzipunzipboto3

Trouble unzipping extremely large files


I've been given a number of files that are zipped up, but unzipped are 30GB+ and have been zipped in Windows. I am trying to create a system using EC2 instances to unzip these, but I keep maxing out memory (error IOError: [Errno 28] No space left on device). My unzip script is as follows:

import boto3
from boto3.s3.transfer import S3Transfer
from zipfile import ZipFile as zip
import ec2metadata
import re

s3 = boto3.client('s3')
transfer = S3Transfer(s3)


def get_info():
    userdata = re.findall(r"\=(.*?) ", ec2metadata.get('user-data'))
    global dump_bucket 
    dump_bucket = userdata[0]
    global bucket
    bucket = userdata[1]
    global key
    key = userdata[2]
    return dump_bucket, bucket, key

def unzipper(origin_bucket, origin_file, dest_bucket):
    s3.download_file(bucket, key, '/tmp/file.zip')

    zfile = zip('/tmp/file.zip')

    namelist = zfile.namelist()

    for filename in namelist:
        data = zfile.read(filename)
        f = open('/tmp/' + str(filename), 'wb')
        f.write(data)
        f.close()

    transfer.upload_file('/tmp/' + str(filename), dump_bucket, namelist[0])

def main():
    get_info()
    unzipper(dump_bucket, bucket, key)

main()

Are there any better ways to unzip the file? I tried streaming it, but that wouldn't work most likely due to the way it was initially compressed.


Solution

  • I was able to solve this by increasing the available memory, part of the issue was also coming from the encoding. So had to change default encoding to latin-1