예제 #1
0
파일: fileutils.py 프로젝트: TauPan/bdec
def unzip(zipfilepath, extractdir):
    """Unzip zipfilepath into extractdir."""
    z = zipfile.ZipFile(zipfilepath, mode="r")
    for info in z.infolist():
        filename = os.path.join(extractdir, info.filename)
        try:
            dir = os.path.dirname(filename)
            aglogging.debug(fileutilsLogger, "Creating dir %s" % dir)
            os.makedirs(dir) # do we have to worry about permissions?
        except:
            pass
        if os.path.isdir(filename):
            continue
        aglogging.debug(fileutilsLogger,\
                       ("Writing arcfile %s to %s" % (info.filename, filename)))
        f = open(filename, "w")
        f.write(z.read(info.filename))
        f.close()
예제 #2
0
파일: fileutils.py 프로젝트: TauPan/bdec
def zip(zipfilepath, basedir=None, files=None):
    """Zip all files in files and save zip as zipfilepath. If files is None, zip all files in basedir. For all files to be zipped, if they are relative to basedir, include the relative path in the archive."""
    
    if not files and not basedir:
        raise AssertionError("Either 'basedir' or 'files' must be set")
            
    if not files:
        aglogging.debug(fileutilsLogger,\
                        "Looking for files to zip in %s" % basedir)
        files = getAllExistingFiles(basedir)
    else:
        # removes files that don't exist and gets abs for each
        files = getAllExistingFiles(files) 

    if len(files) == 0:
        aglogging.debug(fileutilsLogger, "No files to zip, nothing to do")
        return
    
    z = zipfile.ZipFile(zipfilepath, mode="w", compression=zipfile.ZIP_DEFLATED)

    try:
        for file in files:
            arcname = None
            if basedir:
                arcname = getRelativePath(file, basedir)
            if not arcname:
                arcname = file
                aglogging.debug(fileutilsLogger,\
                                "%s: adding %s with arcname %s" %\
                                (zipfilepath, file, arcname))
            z.write(file, arcname)
    finally:
        z.close()