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. iOS:集成ijkplayer视频直播

    介绍: ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS, 网上也有很多集成说明, 但是个人觉得还是不够详细, 在这里详细的讲一下在 iOS 中如何集 ...

  2. ionic 报错%1 is not a valid Win32 application

    Fixed the problem by installing python version 3.0 and above will do下载Python3.0或以上版本 python官网传送门:htt ...

  3. A股回归牛市?

    A股回归牛市? 国际知名投行摩根士丹利(Morgan Stanley)预计,中国股市将恢复牛市大涨行情. 该行预计上证综指明年底将报4,400点,较目前的3,241点水平涨36%.预计每股盈余(EPS ...

  4. 微信开发教程 Yank.WeiXin.Robot

    http://www.cnblogs.com/yank/category/539657.html

  5. RouteOS软路由HotSpot热点认证网关添加白名单和黑名单

    1.添加白名单和黑名单地址池 白名单IP地址池 172.18.10.0/24 黑名单IP地址池 172.18.20.0/24 2.添加IP网关 白名单网关 172.18.10.0/24 黑名单网关 1 ...

  6. webService调用的N种方式

    一.服务器端代码 新建一个web Service Project工程(貌似普通的java工程也可以,这里不太明白各种webService的框架有什么区别),建立如下的类: package com.bo ...

  7. VS2010 AlwaysCreate

    VS2010 解决方案里有两个工程,每次按F7编译都会链接一次.链接完成再按F7又会链接一次..... 提示: 正在创建“Debug\testb.unsuccessfulbuild”,因为已指定“Al ...

  8. 学习jsp(1)

    我的开发环境是:jdk1.7;netbean ;tomcat7. 尝试着写了第一个程序: ublic class myServlet extends HttpServlet{    @Override ...

  9. Website English Comments

    幻灯新闻下方的广告 Slide news at the bottom of the advertisement 人才招聘 recruitment 左/右侧推荐区 The left/right side ...

  10. 企业Openvpn环境部署

                   企业Openvpn环境部署                                         作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任 ...