Repack Archives (2005)

Extracts and repacks .pak archive files using pkzipc. Recursively finds all .pak files, extracts them to temporary directories, then repacks them. Useful for recompressing archives or fixing archive structure issues. Requires FAR Manager's pkzipc.exe archiver.

Usage:

perl repack.pl <input_directory> <output_directory>

Finds all .pak files recursively, extracts and repacks them using pkzipc.exe from FAR Manager.

Source Code:

#!/usr/bin/perl
#
sub tree {
	my(@filenames);
	my($root) = $_[0];
	print "\nexploring $root ";
	opendir ROOT, $root;
	my(@filelist) = readdir ROOT;
	closedir ROOT;
	foreach $name (@filelist) {
		if ($name ne '.' and $name ne '..') { 
			$name = $root . "\\" . $name;
			if (-f $name  && ($name =~ /.pak$/i)) {
				@filenames = (@filenames, $name);
				$i++;
				print "."  if ($i % 100 == 0);
			}			
			@filenames = (@filenames, tree($name)) if (-d $name); 
		}
	}
	print "\n$root: $i files...\n";
	return @filenames;
}

$outdir = $ARGV[1] or die "outdir fail...";

@filenames = tree($ARGV[0]) or die "indir fail...";;

system("md \"$outdir\"") if (!-d $outdir);

foreach $filename (@filenames) {
	($name, @path) = reverse(split(/\\/, $filename));
	($drive, @outpath) = reverse(@path);
	$outpath = join("\\", @outpath);

	print "extracting $filename to $outdir\\$outpath: \n";

	system("md \"$outdir\\$outpath\\$name.tmp\"") if (!-d "$outdir\\$outpath\\$name.tmp");
	system("\"%FARHOME%\\Archivers\\pkzipc.exe\" -ext -dir -over=all -nozip -mask=none -times=mod $filename \"$outdir\\$outpath\\$name.tmp\"");

	print "repacking $filename from $outdir\\$outpath: \n";

	system("\"%FARHOME%\\Archivers\\pkzipc.exe\" -add -move -attr=all -dir=relative -nozip \"$outdir\\$outpath\\$name\" \"$outdir\\$outpath\\$name.tmp\\*\"");
	system("rd  /s /q \"$outdir\\$outpath\\$name.tmp\"") if (-d "$outdir\\$outpath\\$name.tmp");
}