ftp import zip file with csv data, getting “string

2019-08-10 14:26发布

I'm importing zip files through ftp like this

files = []
Net::FTP.open(SERVER_DOMAIN) do |ftp|
  ftp.passive = true
  ftp.login(FTP_USERNAME, FTP_PASSWORD)
  day_range = (Time.now - DAY_RANGE.hours)..Time.now
  file_names = ftp.nlst
  file_names.each do |file_name|
    if day_range.cover?(ftp.mtime(file_name))
      file = ftp.getbinaryfile(file_name,nil, blocksize=1024)
      files << file
    end
  end
end
files

and then I'm passing each file into this block.

def unzip file
# I've tried putting `file`, which is really just content, into a `Tempfile`
# and passing that into `Zip::ZipFile.open` but I get an error then too
  data = ""
  Zip::ZipFile.open(file) do |zipfile|
    debugger
    zipfile.each do |entry|
      data = entry.file.read(entry.name)
    end
  end
end

Before I get to the debugger I'm getting this error

ArgumentError: string contains null byte

the tail end of the contents of file look like this

?*\xA2\xCD`\x12\xB6\x93\x94\xDE\xF0\xA1\x93E8\xF3o\x11\x02\xF6\xD9.%}\x19\xC7I)\xD9\x0F*\x14\xAB\xC4\x8C\x8A[\xF1\xA5\x98\x19N2qd\x9D\x89\xFD\xA0\x81N\\\x88>E\xF3$X\x1DTBbV<\xF9\xB7\xB5V\xCE9\xEB\x84<\xEB\"\xA4\xCA\x15\xFE\x7F\x01PK\a\b\x15\xAEX\x99\xEA\xF0\x02\x00\xF4\xD9)\x00PK\x01\x02\x14\x00\x14\x00\b\b\b\x00\x97F<D\x15\xAEX\x99\xEA\xF0\x02\x00\xF4\xD9)\x00\e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00KS_Google_Bing-20140128.csvPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00I\x00\x00\x003\xF1\x02\x00\x00\x00"

However, it works from a local zip!

If I pass in the same zip file from my public directory everything works fine.

def unzip file
  data = ""
  Zip::ZipFile.open("public/my.zip") do |zipfile| # notice I'm using a local file here instead of the passed in ftp file
    zipfile.each do |entry|
      data = entry.file.read(entry.name)
    end
  end
end

And if I look at the contents of the local file public/my.zip with code like this:

local_file = File.open("public/my.zip")

the tail end of local_file.read looks like this.

FB\xC9ã\u000F\xE1\xEDB*\x8A\xDBT\x94\u0004\xAC;\u05CE\xB7(?*\xA2\xCD`\u0012\xB6\x93\x94\xDE\xF0\xA1\x93E8\xF3o\u0011\u0002\xF6\xD9.%}\u0019\xC7I)\xD9\u000F*\u0014\xABČ\x8A[\xF1\xA5\x98\u0019N2qd\x9D\x89\xFD\xA0\x81N\\\x88>E\xF3$X\u001DTBbV<\xF9\xB7\xB5V\xCE9\xEB\x84<\xEB\"\xA4\xCA\u0015\xFE\u007F\u0001PK\a\b\u0015\xAEX\x99\xEA\xF0\u0002\u0000\xF4\xD9)\u0000PK\u0001\u0002\u0014\u0000\u0014\u0000\b\b\b\u0000\x97F<D\u0015\xAEX\x99\xEA\xF0\u0002\u0000\xF4\xD9)\u0000\e\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000KS_Google_Bing-20140128.csvPK\u0005\u0006\u0000\u0000\u0000\u0000\u0001\u0000\u0001\u0000I\u0000\u0000\u00003\xF1\u0002\u0000\u0000\u0000"

So neither of the look like the raw binary of the zip file, which looks like this on the tail end:

38f3 6f11 02f6 d92e 257d 19c7 4929 d90f 2a14 abc4 8c8a 5bf1 a598 194e 3271 649d 89fd a081 4e5c 883e 45f3 2458 1d54 4262 563c f9b7 b556 ce39 eb84 3ceb 22a4 ca15 fe7f 0150 4b07 0815 ae58 99ea f002 00f4 d929 0050 4b01 0214 0014 0008 0808 0097 463c 4415 ae58 99ea f002 00f4 d929 001b 0000 0000 0000 0000 0000 0000 0000 0000 004b 535f 476f 6f67 6c65 5f42 696e 672d 3230 3134 3031 3238 2e63 7376 504b 0506 0000 0000 0100 0100 4900 0000 33f1 0200 0000 

Any help would be AWESOME!!

1条回答
该账号已被封号
2楼-- · 2019-08-10 14:41

I think the problem is that the function Zip::ZipFile.open() expects a filename (not contents), in your second example you are feeding the filename "my.zip" to the function whereas in the example that errors, it appears you're feeding the contents of the file.

I think this answer will work for what you're trying to do here:
Processing Zip files in Ruby

Zip::ZipFile.foreach(file) do |entry|
    istream = entry.get_input_stream
    data = istream.read
    #...
end
查看更多
登录 后发表回答