perl脚本(压缩文件夹下文件)上传服务器
利用perl写一脚本压缩本地文件夹上传到服务器,具体, #!/usr/bin/perl -w use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); use File::Path; use File::Copy; my $zip = Archive::Zip->new(); use strict; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( time - 604800 );#一周前的时间 my $yyyymmdd = sprintf( "ddd", $year + 1900, $mon + 1, $mday ); my $get_name = 'D:/Notepad'; # 可以改为带参数运行$ARGV[0] my $topath = 'O:/etest/mu'; #$ARGV[1] my @data; opendir( TEMP, "$get_name" ) || die "$!";#读文件夹测试时间 while ( my $file = readdir TEMP ) { if ( $file !~ /^\.\.?$/ ) { chomp $file; if ( -d "$get_name/$file" ) { if ( time_change( ( stat("$get_name/$file") )[9] ) < $yyyymmdd ) { push @data, $file; } } } } closedir(TEMP); #一下是每个文件夹压缩,测试上传成功删除本地文件夹和zip foreach my $j (@data) { $zip = Archive::Zip->new(); $zip->addTree( "$get_name/$j", "$j", sub { /.*/ } ); my $status = $zip->writeToFileNamed("$get_name/$j.zip"); if ( $status == AZ_OK ) { copy( "$get_name/$j.zip", "$topath/$j.zip" ); if ( -e "$topath/$j.zip" ) { rmtree("$get_name/$j"); unlink("$get_name/$j.zip"); } else { unlink("$get_name/$j.zip"); } } } sub time_change { my $tt = shift; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($tt); my $ymd = sprintf( "ddd", $year + 1900, $mon + 1, $mday ); } $mon + 1, $mday ); my $get_name = 'D:/Notepad'; # 可以改为带参数运行$ARGV[0] my $topath = 'O:/etest/mu'; #$ARGV[1] my @data; opendir( TEMP, "$get_name" ) || die "$!";#读文件夹测试时间 while ( my $file = readdir TEMP ) { if ( $file !~ /^\.\.?$/ ) { chomp $file; if ( -d "$get_name/$file" ) { if ( time_change( ( stat("$get_name/$file") )[9] ) < $yyyymmdd ) { push @data, $file; } } } } closedir(TEMP); #一下是每个文件夹压缩,测试上传成功删除本地文件夹和zip foreach my $j (@data) { $zip = Archive::Zip->new(); $zip->addTree( "$get_name/$j", "$j", sub { /.*/ } ); my $status = $zip->writeToFileNamed("$get_name/$j.zip"); if ( $status == AZ_OK ) { copy( "$get_name/$j.zip", "$topath/$j.zip" ); if ( -e "$topath/$j.zip" ) { rmtree("$get_name/$j"); unlink("$get_name/$j.zip"); } else { unlink("$get_name/$j.zip"); } } } sub time_change { my $tt = shift; my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime($tt); my $ymd = sprintf( "ddd", $year + 1900, $mon + 1, $mday ); }
用户评论