【问题标题】:Archive::Tar module backs up files and folders, but not files in foldersArchive::Tar 模块备份文件和文件夹,但不备份文件夹中的文件
【发布时间】:2019-11-16 23:45:01
【问题描述】:

基本问题。

我想使用 Perl 的 Archive::Tar 模块来备份东西。我想使用Archive::Tar 来备份文件和文件夹以及文件夹中的文件。使用我当前的代码,前两个有效,但第三个无效。

重现步骤:

  1. 创建示例目录。
  2. 在示例目录中,创建两个目录和一个 .txt 文件。
  3. 使用 vim,在 .txt 文件中写入任何内容。
  4. 在示例目录的其中一个目录中,重复步骤二和三。
  5. 在示例目录中,创建一个 perl 脚本。
  6. 将以下代码复制到脚本中。
  7. 运行脚本。在框中,键入 .txt 文件的名称和两个目录。
  8. 按下大压缩按钮。
  9. 在压缩目录中打开新的 .tbz 文件。

代码。

use strict; use warnings;

use Archive::Tar;
use Tk;

my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;

my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;

$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;

MainLoop;

sub compress {
    my $input = $inputEntry -> get;
    my @input;
    unless ( $input !~ m/ / ) {
        @input = split ( m/ /, $input );
    } else {
        @input = ( $input );
    }
    Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )    
}

【问题讨论】:

  • Iirc,您必须明确列出每个文件。仅传递一个目录不会自动递归地包含该目录中的所有内容。
  • 啊。我想我明白了。

标签: perl archive


【解决方案1】:

您的重现步骤不清楚,更多示例可能会有所帮助,但您的脚本似乎对我来说工作正常。我能够成功地将文件夹和文件以及文件夹中的文件添加到存档中。对于文件夹中的每个文件,我必须输入整个相对路径,例如。 'sample/file1.txt'。

您可能正在寻找一种在文件夹中自动添加文件的方法。像这样的东西可以解决问题:

use strict; use warnings;

use Archive::Tar;
use Tk;

my $mw = MainWindow -> new;
$mw -> Label ( -text => "Please type the files you wish to backup, separated by spaces." ) -> pack;

my $inputEntry = $mw -> Entry ( -width => 30 );
$inputEntry -> pack;

$mw -> Button ( -text => "Compress!", -command => sub { compress() } ) -> pack;

MainLoop;

sub compress {
    my $input = $inputEntry -> get;
    my @input; 
    my @dirfiles;
    unless ( $input !~ m/ / ) {
        @input = split ( m/ /, $input );
    } else {
        @input = ( $input );
    }

    foreach(@input) { if(-d $_) { push(@dirfiles,glob "'${_}/*'"); } }
    push(@input,@dirfiles);

    Archive::Tar -> create_archive ( "TEST.tbz", COMPRESS_BZIP, @input )
}

我认为仍然存在一些问题,例如:此脚本似乎在名称中包含空格的文件/文件夹上失败,即使在转义或引用时也是如此。

【讨论】:

  • 谢谢!这行得通!您能否解释一下添加的行的作用,以便我可以从错误中吸取教训?
  • 感谢您的勾选。 There are three added lines. The first creates another local array (@dirfiles), the second grabs a list of files from any entry it detects is a folder and 'saves' that to the @dirfiles array. The third line merges the @dirfiles array with the '@input' array. 你可以在这里找到更多关于 perl glob 的信息:perldoc.perl.org/functions/glob.html
  • 这只会直接获取文件夹中的文件,而不是文件夹中文件夹中的文件;对于你想要的 File::Find
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
相关资源
最近更新 更多