在Ruby中使用奇偶校验归档工具处理多部分.rar文件时出现问题

vq8itlhq  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(104)

使用par2j64.exe创建奇偶校验存档工具,我用Ruby编写了一个脚本,为单个.rar文件创建奇偶校验存档,但也希望能够将其用于多部分.rar文件。
这适用于单个.rar文件:

# Set parameters for par2j64
parity_tool = "par2j64.exe"
redundancy_percent = "15"
par2_files = "8"
working_directory = "G:/Working"

# Find all RAR files in the specified working directory
rar_files = Dir.glob(File.join(working_directory, "*.rar"))

if rar_files.empty?
puts "No RAR files found in the specified working directory."
else
# Iterate over each RAR file
rar_files.each do |rar_file_path|
# Extract the filename from the file path
rar_file_name = File.basename(rar_file_path)

# Create parity archives for the RAR file
puts "Creating parity archives for #{rar_file_name}..."
Dir.chdir(working_directory) do
  system("#{parity_tool} c /rr#{redundancy_percent} /rf#{par2_files} /rd1 \"#{rar_file_name}.par2\" \"#{rar_file_path}\"")
end
puts "Parity archives created for #{rar_file_name}."
puts
end
end

字符串
这个DOS脚本代码可以工作,但我不知道如何将它转换为Ruby脚本:

rem Search the first file of each splitted RAR archive
for %%F in ("*.part1.rar" "*.part01.rar" "*.part001.rar") do (
    call :SUB_CREATE "%%~nF"
)

echo Parity archive creation complete.
pause

rem Exit script before sub routine
exit

:SUB_CREATE
rem Sub-routine to create PAR files for multiple input files

echo Creating parity archives for multiple %~n1...
%PARITY_TOOL% c /rr%REDUNDANCY_PERCENT% /rf%PAR2_FILES% /rd1 "%~n1.par2" "%~n1.part*.rar"
echo Parity archives created for multiple %~n1.
echo
rem exit subroutine
exit /B


我在Ruby中尝试过这样的事情,但没有成功:

# Set parameters
parity_tool = "par2j64.exe"
redundancy_percent = "15"
par2_files = "8"
working_directory = "G:/Working"

# Search the first file of each splitted RAR archive
Dir.glob("*.part1.rar") do |file|
create_parity(file)
end

Dir.glob("*.part01.rar") do |file|
create_parity(file)
end

Dir.glob("*.part001.rar") do |file|
create_parity(file)
end

puts "Parity archive creation complete."
puts "Press Enter to continue."
gets

# Method to create parity archives for multiple input files
def create_parity(file)
puts "Creating parity archives for multiple #{File.basename(file, ".*")}..."
system("#{parity_tool} c /rr#{redundancy_percent} /rf#{par2_files} /rd1 #{File.basename(file, ".*")}.par2 #{File.basename(file, ".*")}.part*.rar")
puts "Parity archives created for multiple #{File.basename(file, ".*")}."
puts
end

83qze16e

83qze16e1#

好吧,我自己想出来的。我以前也试过这样的方法,但不明白为什么它不起作用。问题是多部分.rar文件中的“.part”作为基本文件名的一部分被包含在内,因此par2j64.exe找不到输入文件。以下Ruby脚本用于为多部分.rar文件创建奇偶校验存档,它假设par2j64.exe的路径已添加到Windows环境变量中,否则需要调用它,包括实际路径:

directory = "."  # Specify the directory where the files           are located

# Search for the .rar files in the directory
rar_files = Dir.glob(File.join(directory, "*.rar"))

if rar_files.empty?
  puts "No .rar files found in the directory."
  exit
end

# Extract the base filename from the first .rar file found
# and split off the .part because it can't be part of the
# base_filename when sent to par2j64
base_filename = File.basename(rar_files.first, ".*")
base_filename_without_parts = base_filename.split(".part").first

# Create the input file pattern
input_files = "#{base_filename_without_parts}.part*.rar"

# Set the output file name
output_file = "#{base_filename_without_parts}.par2"

command = "par2j64.exe c /rr15 /rf8 /rd1 \"#{output_file}\" \"#{input_files}\""
system(command)

字符串

相关问题