TarWriter 抛出 Gem::Package::TarWriter::FileOverflow
TarWriter throws Gem::Package::TarWriter::FileOverflow
我想从一堆文件中生成一个 tar。
out_file = File.new('some.tar', 'w')
tar = Gem::Package::TarWriter.new out_file
attachments = #Array of attachment objects
attachments.each{|a|
file = Attachment.new(a).read_file #returns a String
file.force_encoding('UTF-8')
tar.add_file_simple( a[:filename], 777, file.length) do |io|
io.write(file)
end
}
Gem::Package::TarWriter::FileOverflow - You tried to feed more data
than fits in the file.
有谁知道为什么会发生这种情况以及如何解决它?
String#length
returns 字符串中的字符数。由于 UTF-8 字符可以用多个字节表示,因此字符串的字节大小通常更大。
TarWriter
现在要求文件大小以字节为单位。因此,如果您在文件中使用除普通 ascii 字符之外的任何其他字符,它将溢出。
要解决这个问题,您应该将 file.bytesize
传递给 add_file_simple
方法而不是 file.size
。
我想从一堆文件中生成一个 tar。
out_file = File.new('some.tar', 'w')
tar = Gem::Package::TarWriter.new out_file
attachments = #Array of attachment objects
attachments.each{|a|
file = Attachment.new(a).read_file #returns a String
file.force_encoding('UTF-8')
tar.add_file_simple( a[:filename], 777, file.length) do |io|
io.write(file)
end
}
Gem::Package::TarWriter::FileOverflow - You tried to feed more data than fits in the file.
有谁知道为什么会发生这种情况以及如何解决它?
String#length
returns 字符串中的字符数。由于 UTF-8 字符可以用多个字节表示,因此字符串的字节大小通常更大。
TarWriter
现在要求文件大小以字节为单位。因此,如果您在文件中使用除普通 ascii 字符之外的任何其他字符,它将溢出。
要解决这个问题,您应该将 file.bytesize
传递给 add_file_simple
方法而不是 file.size
。