| Category: | Utility Scripts |
| Author/Contact Info | |
| Description: | A simple script that can be used to split files. |
| For the chunk size parameter , you can specify megabytes or gigabytes. Here is an example on how the script can be runned: perl split.pl linux.iso 100M linux_chunks where linux.iso is a cd image we're splitting in 100MB chunks , and we're storing them in the directory linux_chunks. Hope this helps ! use strict;
use warnings;
my $f=shift || die "File that your want to split\n";
my $chunk_size=shift || die "Chunk size\n";
my $dir=shift || die "Directory where the chunks are stored\n";
my $fsize=-s $f;
my $size=0;
my $buffer;
my $BUFFER_SIZE=4096;
my $nr_of_chunks=0;
my $nr=0;
if($chunk_size=~/(\d+)([mMgG])/) {
my $numb=$1;
my $tp=$2;
SW: {
($tp eq "g" || $tp eq "G") && do {
$numb*=1024*1024*1024;
$size=$numb;
last SW;
};
($tp eq "m" || $tp eq "M") && do {
$numb*=1024*1024;
$size=$numb;
last SW;
};
}
}
else {
$size=$chunk_size;
}
open(F,$f) || die "OPEN : $!\n";
binmode(F);
if(! -e $dir) {
mkdir($dir) || die "DIR : $!\n";
}
chdir($dir) || die "CHDIR : $!\n";
FILE:{
if(($fsize - $size)<=0) {
my $count=0;
print "Creating file $nr\n";
open(C,">$nr") || die "CREATE : $!\n";
binmode(C);
while($count != $fsize) {
$count+=read(F,$buffer,$BUFFER_SIZE);
print C $buffer;
}
close C;
last FILE;
}
else {
my $count=0;
print "Creating file $nr\n";
open(C,">$nr") || die "CREATE : $!\n";
binmode(C);
while($count != $size) {
$count+=read(F,$buffer,$BUFFER_SIZE);
print C $buffer;
}
close C;
$fsize-=$size;
$nr++;
redo FILE;
}
}
print "Over & Out\n";
and the script needed to join the files
use strict;
use warnings;
my $dir=shift || die "Directory where the chunks are stored\n";
my $out=shift || die "Name under which we join the chunks\n";
my @files;
my $buffer;
opendir(D,$dir) || die "OPENDIR : $!\n";
@files=grep { /^\d+$/ } readdir(D);
closedir(D);
chdir($dir) || die "CHDIR : $!\n";
open(F,">../$out") || die "CREATE : $!\n";
binmode(F);
@files=sort { $a <=> $b } @files;
for(@files) {
open(C,$_) || die "OPEN : $!\n";
binmode(C);
while(read(C,$buffer,4096)) {
print F $buffer;
}
print "I joined chunk nr. $_\n";
close C;
}
print "File $out is complete now\n";
close F;
|
|
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File splitting script
by jdporter (Paladin) on Apr 21, 2007 at 17:29 UTC | |
|
Re: File splitting script
by jwkrahn (Abbot) on Apr 21, 2007 at 21:05 UTC | |
by Alien (Monk) on Apr 21, 2007 at 21:51 UTC |