简体   繁体   English

在tarfile中区分来自不同驱动器的文件

[英]Discriminate files from different drives in tarfile

I am trying to archive and compress multiple directories spread along multiple drives using the tarfile library. 我正在尝试使用tarfile库压缩和压缩分布在多个驱动器上的多个目录。 The problem is that tarfile merges the paths even if two files are stored in different drives. 问题是即使两个文件存储在不同的驱动器中, tarfile也会合并路径。 For example: 例如:

import tarfile
with tarfile.open(r"D:\Temp\archive.tar.gz", "w:gz") as tf:
    tf.add(r"C:\files\foo")
    tf.add(r"D:\files\bar")

Will create an archive containing the following files: 将创建一个包含以下文件的档案:

archive.tar.gz
└─ files
   ├─ foo
   └─ bar

Is there a way of creating this? 有没有办法创建这个?

archive.tar.gz
├─ C
|  └─ files
|     └─ foo
└─ D
   └─ files
      └─ bar

You'll need to use tarfile.addfile() instead of tarfile.add() : 您需要使用tarfile.addfile()而不是tarfile.add()

With TarInfo you can specify the filename which will be used in the archive. 使用TarInfo,您可以指定将在存档中使用的文件名。

Exemple : 范例:

with open(r"C:\files\foo", "rb") as ff:
    ti = tf.gettarinfo(arcname="C/files/foo", fileobj=ff)
    tf.addfile(ti, ff)

Or maybe, a faster solution : 或者,也许是一个更快的解决方案:

tf.add('/path/to/dir/to/add/', arcname='C/files')
tf.add('/path/to/otherdir/to/add/', arcname='D/files')

Thanks Loïc for your answer, it helped me find the actual answer I was looking for. 感谢Loïc的回答,它帮助我找到了想要的实际答案。 (And also made me waste about a hour because I got really confused with the linux and windows style paths you mixed up in your answer)... (这也使我浪费了大约一个小时,因为我对您在答案中混淆的Linux和Windows样式路径感到非常困惑)...

import os
import tarfile

def create_archive(paths, arc_paths, archive_path):
    with tarfile.open(archive_path, "w:gz") as tf:
        for path, arc_path in zip(paths, arc_paths):
            tf.add(path, arcname=arc_path)

def main():
    archive = r"D:\Temp\archive.tar.gz"
    paths = [r"C:\files\foo", r"D:\files\bar"]
    # Making sure all the paths are absolute.
    paths = [os.path.abspath(path) for path in paths]
    # Creating arc-style paths
    arc_paths = [path.replace(':', '') for path in paths]
    # Create the archive including drive letters (if in windows)
    create_archive(paths, arc_paths, archive)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM