我有一个脚本,输出基于输入数据的网址。每一行输入生成大约20个URL。然后使用此输出上载到Akamai Content Control。但是,Akamai对文件大小的限制设置为50KB。
我知道我可以写一个URL,然后检查文件大小。如果文件大小超出缓冲区,请关闭该文件并创建一个新文件,然后重复该操作。
我最终好奇的是,在Ruby中是否有一种更优雅的方法来处理这个问题,而开销/代码更少。
谢谢
发布于 2015-10-05 22:52:04
在写入每一行之前,检查文件大小+行大小是否小于限制。写入文件或相应地创建新的文件。
index = 2;
f = File.new('output_file.txt', 'w')
while(there_is_new_output_to_write) do #get new line
if(f.size + output_line.size > LIMIT) do #if total size is too big
Fileutils.cp('output_file.txt','output_file' + index + '.txt') #copy
index = index + 1 #increment index for new file
f = File.new('output_file.txt', 'w') #override output file
end
f.puts(output_line) #write new line
end
#here You will have index -1 files
#that is if index is 4, then you will have output_file.txt, output_file2.txt, output_file3.txt,
#but NOT output_file4.txt!或者,如果这不是问题,您可以使用一些好的归档工具来为您拼接50kb大小的文件。
https://stackoverflow.com/questions/32951436
复制相似问题