I want to change the file name in the zip file.

Asked 2 years ago, Updated 2 years ago, 35 views

#zip filename
zip_file_name = "test.zip"
# zip file path
zip_file_path = File.join Rails.public_path, zip_file_name
# a file to be included in a zip file
file="xxxxxxx/xxxxx/test.txt"

`zip-jm#{zip_file_path}#{file}`1
FileUtils.chmod (0664, zip_file_path) ②
US>'rm#{file}'3

で Compressed test.txt as test.zip,
②Change the permission of the test.zip file in
③Delete the test.txt file in

Do not modify the file to be included in the zip file. ①Would it be possible to change the name of the file to test2.txt to include in the zip file created in ?

ruby

2022-09-30 17:09

2 Answers

How about using Ruby's Zip library instead of the zip command?

https://github.com/rubyzip/rubyzip

The first argument for Zip::File#add is the filename in Zip and the real filename that you want to store the second argument.


2022-09-30 17:09

The question is "Initial state (data given): There is a "test.zip" file that compresses the "test.txt" file (the "test.txt" file has already been deleted and does not exist).
Desired final state (resulting data): A file with the same content as "text.txt" and the filename "test2.txt" replaced the "test.txt" file
"I think you want to know the operation procedure that meets ""

""

How about the following?
1. Extract test.zip to extract the "test.txt" file using the $unzip test.zip command. 2. Delete the "test.txt" file from test.zip using the $zip-d test.zip test.txt command
3.Change the filename from "test.txt" to "test2.txt" using the $mv test.txt test2.txt command
4. Add the "test2.txt" file to the "test.zip" file with the $zip test.zip test2.txt command


2022-09-30 17:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.