コード例 #1
0
def zip_(args):
    if args.recursive:
        entries = []
        for root_entry in args.entries:
            for directory, _, files in os.walk(root_entry):
                entries.append(directory)
                entries.extend([os.path.join(directory, f)
                                for f in files])
        args.entries = entries

    if args.include or args.exclude:
        include = bool(args.include)
        patterns = [re.compile(pattern)
                    for pattern in args.include or args.exclude]
        filtered = []
        for entry in args.entries:
            for pattern in patterns:
                if pattern.search(entry):
                    if include:
                        filtered.append(entry)
                else:
                    if not include:
                        filtered.append(entry)
        args.entries = filtered

    with zipfile.ZipFile(args.zip_file, 'a') as z:
        for entry in args.entries:
            if args.verbose:
                print("Archiving:", entry)
            arcname = convert(entry, args.from_, args.to)
            z.write(entry, arcname)
コード例 #2
0
def unzip(args):
    if args.include or args.exclude:
        include = bool(args.include)
        patterns = [re.compile(pattern)
                    for pattern in args.include or args.exclude]
    else:
        include = True
        patterns = None

    with zipfile.ZipFile(args.zip_file) as z:
        if args.list:
            buffer = StringIO.StringIO()
            _stdout = sys.stdout
            sys.stdout = buffer
            z.printdir()
            sys.stdout = _stdout
            buffer.seek(0)
            print(convert(buffer.read(), args.from_, args.to))
            return

        if args.password:
            z.setpassword(args.password)

        for zinfo in z.infolist():
            file_name = convert(zinfo.filename, args.from_, args.to)

            if args.verbose:
                print("Entry:", file_name)

            if args.entries and file_name not in args.entries:
                if args.verbose:
                    print("Not specified:", file_name)
                continue

            if file_name.startswith(os.sep) and not args.force:
                raise ValueError("Absolute path: %s" % file_name)

            if patterns:
                for pattern in patterns:
                    if pattern.search(file_name):
                        if include:
                            break
                    else:
                        if not include:
                            break
                else:
                    if args.verbose:
                        print("Excluded or not included:", file_name)
                    continue

            # mkdir -p
            dir_name = os.path.dirname(file_name)
            try:
                os.makedirs(dir_name)
            except os.error:
                pass

            if not file_name.endswith(os.sep):
                bin = z.read(zinfo)
                if args.verbose:
                    print("Extracting:", file_name)
                with open(file_name, 'wb') as f:
                    f.write(bin)