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. 每日目标——HTML 头部标签学习 2015-8-27

    <head> <title>bp</title> <meta http-equiv="Content-Type" content=&quo ...

  2. js运算之比较大小

    1.大于>和小于< var box = 3 > 2;//关系运算符大多返回的是一boolean值. alert(box); //true 1.2不同类型的数据比较 var box = ...

  3. asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法

    最近项目在 IE 11 下<asp:LinkButton> 点击出现 “__doPostBack”未定义”,经过一番google,终于知道了原因:ASP.NET 可能无法辨识出一些浏览器的 ...

  4. Android 用Animation-list实现逐帧动画

    第一步:先上图片素材,以下素材放到res/drawable目录下: http://blog.csdn.net/aminfo/article/details/7847761 图片素材: 文件名称: ic ...

  5. iOS - (懒加载)

    今天很坑爹,做界面的时候,tableview 明显做了复用了,数组也做了懒加载了,获取数据前也把数组给清空了,但是每次获取数据刷新表格的时候,数据确重复覆盖了(重复创建),后来给 cell 加了个白色 ...

  6. docker note

    docker --bip="10.1.42.1/16" -d 挂载宿主机目录 Docker支持挂载宿主机目录,支持宿主机目录和容器之间文件目录进行映射,彼此共享: docker r ...

  7. Hadoop2.2.0 第一步完成MapReduce wordcount计算文本数量

    1.完成Hadoop2.2.0单机版环境搭建之后需要利用一个例子程序来检验hadoop2 的mapreduce的功能 //启动hdfs和yarn sbin/start-dfs.sh sbin/star ...

  8. UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

    题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...

  9. 20145207 《Java程序设计》第4周学习总结

    前言 又到了大家最喜欢的前言时间,哈哈哈.我这个人啊,就是比较爱闲聊.正式在学校呆的第一天时间就在这里敲代码,自己都觉得自己伟大.不过好无聊呀....这周的内容说实话讲我还是挺感兴趣的,因为书上的例子 ...

  10. ACM之Java速成(1)

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner cin = new Sc ...