Compress a Folder/Directory via Perl5

tested in Windows, Mac OS X, Ubuntu16.04

#!/usr/bin/perl
#压缩指定目录
#nultiple folder
use IO::Dir;
use Archive::Tar;
use v5.16; die "Give me some Folder\n" if ! @ARGV;
&tar_bz2(@ARGV); sub tar_bz2 {
my @haystack = ();
my $fname = join ',', map { $_=~s/\/$//;$_ } @_;
#the compressed file name
$fname = @_.";".$fname;
while(my $dir = shift){ #iterate the given list
$dir =~ s|/$||;
if (! -d $dir){
push @haystack, $dir;
#not a dir/folder, file hodoooor
next
}
sub haystack {
my $dir = shift; # current dir name
my $pat = shift; # under where
my $d = IO::Dir->new("$dir");
if (defined($d)) {
chdir $dir;
while (defined($_ = $d->read)) {
next if ($_ =~ /^\.\.?$/);
if (-f "$_"){
push @haystack, "$pat/$dir/$_";
}
if (-d "$_") {
chmod 0755, "$_";
&haystack($_,"$pat/$dir");
# __SUB__ not work in windows
# no matter `use feature "current_sub"`
# or `use v5.16`
# run under perl v5.18.2
}
}
chdir("..");
}
}
&haystack("$dir",'.');
}
$fname =~ s/[^A-Za-z0-9_\-\.;]+/_/g;
$fname = substr($fname,0,15) if $fname > 14;
# create a Archive::Tar object
my $tar = Archive::Tar->new;
# add files
$tar->add_files(@haystack) or die "$!";
# say STDERR "$_" for (@haystack);
$tar->write("$fname.tbz",COMPRESS_BZIP);
say "Saved to $fname.tbz";
}

So,

$ perl ./backupRenLab.pl font/
Saved to 1;font.tbz font/
|-- A/
|-- A File 2.txt
|-- AFile1.txt
|-- B/
|-- C/
|-- CFile.txt
The sturcture

More,

  • Options Getopt::Long -o(outdir),-t(compress method,gzip bz2,7z,xz),-f(tar filename), -l(compress level)

Compress a Folder/Directory via Perl5的更多相关文章

  1. Compress a folder using powershell

    There are many ways to compress a folder using powershell: Method 1: Using System.IO.Compression and ...

  2. CentOS 7, Attempting to create directory /root/perl5

    By francis_hao    Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...

  3. Managing IIS Log File Storage

    Managing IIS Log File Storage   You can manage the amount of server disk space that Internet Informa ...

  4. 第十三章:Python の 网络编程进阶(二)

    本課主題 SQLAlchemy - Core SQLAlchemy - ORM Paramiko 介紹和操作 上下文操作应用 初探堡垒机 SQLAlchemy - Core 连接 URL 通过 cre ...

  5. Ubuntu notes

    ubuntu notes Table of Contents 1. backup data 2. Basics Ubuntu 3. Install, uninstall packages 4. Bas ...

  6. [.NET] 利用 async & await 进行异步 IO 操作

    利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html  序 上次,博主 ...

  7. C# XMLDocument

    今天开发一个WPF模块需要本地化保存一些用户设置,鉴于数据量不大,用XML. (要是再小的话可以用Resources 和 Settings). 清晰简短教程移步:http://bdk82924.ite ...

  8. C#写入日志信息到文件中

    为了在服务器上运行程序及时的跟踪出错的地方,可以在必要的地方加入写日志的程序. string folder = string.Format(@"D:\\{0}\\{1}", Dat ...

  9. BlogEngine2.9模仿yahoo滚动新闻Widget

    widget.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeFile="w ...

随机推荐

  1. 使用oracle外部表进行数据泵卸载数据

    数据泵卸载Oracle9i引入了外部表,作为向数据库中读取数据的一种方法.Oracle 10g则从另一个方向引入了这个特性,可以使用CREATE TABLE语句创建外部数据,从而由数据库卸载数据.从O ...

  2. kernel 模块与简单 hello 模块

    Kernel 模块与简单 hello 模块 kernel 模块的简介 Linux 内核进行扩展时,例如编写驱动程序.netfilter功能等,最方便的方式是通过编写模块,然后加载到内核中.由于 ker ...

  3. 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数

    2.4.1 用NPOI操作EXCEL关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数   NPOI教程:http://www.cnb ...

  4. mysql 授权 user@'%' 为什么登陆的时候localhost 不行呢???

    公司业务服务器还没迁移到阿里云上的时候,创建的一个用户明明是所有的,但是本机登陆就是不行,一直也搞不懂原因 今天才知道 原来 %不包括 localhost mysql> grant all on ...

  5. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?         本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...

  6. mysql-proxy之奇虎360 Atlas 安装实现mysql读写分离

    官方git https://github.com/Qihoo360/Atlas 参照:http://blog.qixingzhong.com/2013/09/centos-install-atlas. ...

  7. urllib下载文件

    import urllib 1.用urlib.urlretrieve f = urllib.urlretrieve('http://www.baidu.com/img/bdlogo.gif','/tm ...

  8. git提交

    1.git pull 本地已经commit 2.git checkout master 3.git pull 4.git checkout - 5.git merge master 6.git pus ...

  9. Leetcode: Guess Number Higher or Lower II

    e are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess ...

  10. C++Builder 解决绘图闪动问题

    使用双缓冲 Form->DoubleBuffered = true; Panel->DoubleBuffered = true;