简体   繁体   中英

rubyzip and unicode characters in filenames

I am creating zip archive with rubyzip gem and Zip::ZipOutputStream class and have got a problem with unicode letters (cyrillic) - in archive they are presented as question marks like ????? ???? ??.doc ????? ???? ??.doc ????? ???? ??.doc . Does rubyzip support unicode?

I looked at rubyzip methods and it doesn't seem that rubyzip can change the language. It probably uses your computer's default code page. You could use chilkat zip instead as in this example unless you have specific requirements that cannot be addressed by chilkat.

You can use the following snippet to convert UTF-8 to CP437 which cover some unicode chars (just a few). Windows 7 and older assume that filenames are encoded in CP437.

# first normalize the string
normalized_filename = input.mb_chars.normalize.to_s
# then encode in cp437
filename_for_zip = normalized_filename.encode("cp437")
# add file to zip
zipfile.add(filename_for_zip, pdf_file)

You may just run zip directly.

`cd yourfolder; zip archivename file1 file2`

Notice specific quotes. Worked for me on Ubuntu for Cyrillic file names, while rubyzip was generating archive with non-readable file names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM