#!/path/to/perl -w use strict; if($#ARGV!=1){ &usage(); exit(1); } my $filename=$ARGV[0]; my $parts=$ARGV[1]; my $totallength = &getsize($filename); my @part = &calculateparts($totallength, $parts); &readfile($filename, @part); exit(0); sub getsize{ # Read file and get it's TOTAL size open(FILE, $filename) or die "can't open $filename: $!"; binmode(FILE); binmode(STDOUT); my $totallength=0; while (read(FILE, my $buff, 8 * 2**10)) { $totallength = $totallength + length $buff; } close (FILE) || die "Can't close\n"; return $totallength; } sub calculateparts{ # Calculate how many parts are needed # and how many bytes should be in each part my ($totallength, $parts) = @_; my $bytecount = $totallength/$parts; if ($bytecount == int $bytecount){ for (my $count=1; $count <= $parts; $count++){ # print "Part number $count will be $bytecount bytes long\n"; $part[$count] = $bytecount; } } else { my $difference = $totallength - int ($bytecount) * $parts; my $addition = int ($bytecount) + 1; for (my $count=1; $count <= $parts; $count++){ if ($difference > 0){ # print "Part number $count will be $addition bytes long\n"; $part[$count] = $addition; $difference--; } else { # print "Part number $count will be " . int ($bytecount) . " bytes long\n"; $part[$count] = $bytecount; } } } return @part; } sub readfile{ my ($filename, @part) = @_; my $filename2 = $filename; $filename2 =~(s/(.*)\..*/$1/); my $count=1; open (IN,"$filename") || die "can't open $filename for reading $!"; while() { my $rv = read(IN, my $buffer, 4096) || die "Couldn't read from $filename $!\n"; # $rv is the number of bytes read, # $buffer holds the data read if (!$part[$count]) {last;} #print "part[count] is $part[$count]\n"; # if ($rv != $part[$count]){print "Number of bytes read: $rv\n";} my $filename3 = "$filename2.$count"; open (OUT, ">$filename3"); print OUT $buffer; close (OUT); my $pos = tell(IN); print "I'm $pos bytes from the start of $filename.\n"; $count++; } close(IN) || die "Can't close $filename:$!"; } sub usage{ $0 =~(s/.*\\(.*)\..*/$1/); my $Filename = uc($0); if ($^O =~ /Win/) { system"CLS"; } else { system"CLEAR";} print "\nUSAGE:\n"; print "\n\t$Filename \n"; print "\twhere:\n\n"; print "\t is the file to be chopped\n"; print "\t are the number of pieces to chop it into\n"; print "\n"; print "\tERRORLEVEL is set to 0 on Normal Exit\n"; }