Exemple #1
0
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()
Exemple #2
0
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()
Exemple #3
0
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()
Exemple #4
0
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()