rubyzip 2.3.2 read fails on macOS 10.15.7, ruby 3.1.1p18 - ruby

This fails:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
contents = zip.read(filename)
with this error:
Errno::ENOENT: No such file or directory - content.xml
This succeeds:
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
while zip.size == 0
zip = Zip::File.open_buffer(#archive_io, encoding:"UTF-8")
end
contents = zip.read(filename)
Why?
Note that #archive_io is a StringIO instance. 'filename' is an entry in the zip file known to exist of 6253 uncompressed bytes. When using Pry as a debugger, I can simply retype/rerun the open_buffer method and suddenly the zip read works. This lead to me adding the loop to re-attempt the open_buffer - a kludge of a solution.

Related

My gem cannot see a dictionary text file, I cannot get the path

I wrote an implementation of the hangman game. (It is played on the terminal). The game is working fine but I usually have some problems getting my program to open a dictionary txt file from where the word will be generated. Below is my code for generating the word
def word_generator(min, max)
words = File.open("../dictionary.txt"), "r").readlines.map!(&:chomp)
level_words = words.select { |i| i.length >= min && i.length <= max }
random_index = rand(level_words.length)
#game_word = level_words[random_index]
end
This approach works fine when I play my game locally and the dictionary text file is just one directory level away from my ruby file. Here is the problem:
When I package the project as a gem, and install it. It will throw this error in initialize': No such file or directory # rb_sysopen /Users/andeladev/Desktop/paitin_hangman/bin/dictionary.txt (Errno::ENOENT). It will only run fine when I put the text file in the present working directory of the terminal.
How do I go about writing the path in the argument passed to File.open that will tell the program to look for the file in the gem path rather than the present working directory.
Try this:
file_name = File.join(File.dirname(File.expand_path(__FILE__)), '../dictionary.txt')
words = File.open(file_name, "r").readlines.map!(&:chomp)

RubyZip Unzipping .docx, modifying, and zipping back up throws Errno::EACCESS error

So, I'm using Nokogiri and Rubyzip to unzip a .docx file, modify the word/docoument.xml file in it (in this case just change every element wrapped in to say "Dreams!"), and then zip it back up.
require 'nokogiri'
require 'zip'
zip = Zip::File.open("apple.docx")
doc = zip.find_entry("word/document.xml")
xml = Nokogiri::XML.parse(doc.get_input_stream)
inputs = xml.root.xpath("//w:t")
inputs.each{|element| element.content = "DREAMS!"}
zip.get_output_stream("word/document.xml", "w") {|f| f.write(xml.to_s)}
zip.close
Running the code through IRB line by line works perfectly and makes the changes to the .docx file as I needed, but if I run the script from the command line
ruby xmltodoc.rb
I receive the following error:
C:/Ruby193/lib/ruby/gems/1.9.1/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `rename': Permission denied - (C:/Users/Bane/De
sktop/apple.docx20150326-6016-k9ff1n, apple.docx) (Errno::EACCES)
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `on_success_replace'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rubyzip-1.1.7/lib/zip/file.rb:308:in `commit'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/rubyzip-1.1.7/lib/zip/file.rb:332:in `close'
from ./xmltodoc.rb:15:in `<main>'
All users on my computer have all permissions for that .docx file. The file also doesn't have any special settings--just a new file with a paragraph. This error only shows up on Windows, but the script works perfectly on Mac and Ubuntu. Running Powershell as Admin throws the same error. Any ideas?
On my Windows 7 system the following works.
require 'nokogiri'
require 'zip'
Zip::File.open("#{File.dirname(__FILE__)}/apple.docx") do |zipfile|
doc = zipfile.read("word/document.xml")
xml = Nokogiri::XML.parse(doc)
inputs = xml.root.xpath("//w:t")
inputs.each{|element| element.content = "DREAMS!"}
zipfile.get_output_stream("word/document.xml") {|f| f.write(xml.to_s)}
end
Instead you also could use the gem docx, here is an example, the names of the bookmarks are in dutch because, well that's the language my MS Office is in.
require 'docx'
# Create a Docx::Document object for our existing docx file
doc = Docx::Document.open('C:\Users\Gebruiker\test.docx'.gsub(/\\/,'/'))
# Insert a single line of text after one of our bookmarks
# p doc.bookmarks['bladwijzer1'].methods
doc.bookmarks['bladwijzer1'].insert_text_after("Hello world.")
# Insert multiple lines of text at our bookmark
doc.bookmarks['bladwijzer3'].insert_multiple_lines(['Hello', 'World', 'foo'])
# Save document to specified path
doc.save('example-edited.docx')

Ruby - FileUtils.cp deletes file and fails

I am trying to copy a file in ruby using FileUtils#cp
Unfortunately, Ruby is deleting the file and then is unable to copy it because it is missing.
Is this a known bug or something I am doing wrong with the cp method.
src = "/var/tmp/myfile"
dest = "/usr/bin/myfile"
FileUtils.cp(src, dest)
It always complains that src file is missing but when I check it has been deleted. If I recreate the file and set permissions to 777 the file is present, after running the script it is gone and the copy fails
Place the following in a copy_myfile.rb, then run with: sudo ruby copy_myfile.rb
require 'fileutils'
src = "/var/tmp/myfile"
dest = "/usr/bin"
FileUtils.cp(src, dest)
It seems to work for me in Ruby 1.9.3:
my file permission: -rw-rw-r--
require 'fileutils'
=> true
irb(main):002:0> FileUtils.cp 'test.txt', 'text1.txt'
=> nil
The file does get copied.

Downloading a zipfile in binary format using python's ftplib

I have gone through quite a few questions that have been posted which appear to be related, but not entirely the same issue that I am having:
I am using python's ftplib module along with zipfile to download a zip file from ftp in binary format. However, for some reason, the downloaded zip file appears to be in ascii.
I have ensured that a leading / does not exist in the path of the file I am downloading (to match the zip specifications).
outFile = zipfile.ZipFile(local_file_path, 'w')
myftp.retrbinary('RETR %s' %i, outFile.write(i)) #i - target file path on ftp server
This code fails giving me the following error:
st = os.stat(filename)
OSError: [Errno 2] No such file or directory: //$i
I tried adding the 'b' option for binary, but zipfile doesn't seem to like it:
outFile = zipfile.ZipFile(local_file_path, 'wb')
This raises error:
RuntimeError: ZipFile() requires mode "r", "w", or "a"
I am using python v2.6.
What am I doing wrong and how to fix it?
According to python doc (http://docs.python.org/2/library/ftplib.html) seems retrbinary takes a callback as second parameter:
>>> ftp.retrbinary('RETR README', open('README', 'wb').write)
'226 Transfer complete.'
>>> ftp.quit()
Documentation says:
FTP.retrbinary(command, callback[, maxblocksize[, rest]])
Retrieve a file in binary transfer mode. command should be an
appropriate RETR command: 'RETR filename'. The callback function is
called for each block of data received, with a single string argument
giving the data block. [...]
In your example it should be outfile.write (instead of outfile.write(i)).
>>> ftp.retrbinary('RETR %s' % i, outFile.write)

No such file or directory

This is the error.
Atrosity [ Eric-Raios-MacBook ][ ~/dev/rubyscripts ]$ ruby script.rb
script.rb:7:in `read': No such file or directory - sent (Errno::ENOENT)
from script.rb:7:in `lSent'
from script.rb:16:in `<main>'
My Method that is causing the error is:
def lSent
$sent = Set.new(File.read("sent").split(";"))
end
lSent
If I delete this, my script runs but does not output what I want to do.
sent should be a path to a file in your server, such as
$sent = Set.new(File.read("/root/path/file.txt").split(";"))
You're attempting to read a file called "sent", but it doesn't exist in the application's path. Try including the full path to the file.

Resources