質問するログイン新規登録

回答編集履歴

1

追記

2020/05/04 09:50

投稿

winterboum
winterboum

スコア23678

answer CHANGED
@@ -20,4 +20,26 @@
20
20
  }
21
21
  とか
22
22
  ```
23
- これからなら望みのjson組み立てられそうですか?
23
+ これからなら望みのjson組み立てられそうですか?
24
+
25
+ 追記
26
+ dir配下のfileをまとめて階層ごと。
27
+ "basename" は適宜
28
+ Dir.glob(dir+"/**/*{csv,xlsx}") の拡張子も適宜
29
+ ```
30
+ def compress_dir dir
31
+ temp_file = Tempfile.new("basename")
32
+ files = Dir.glob(dir+"/**/*{csv,xlsx}")
33
+ Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
34
+ files.each do |file|
35
+ filename = file.sub(/#{dir}//,"")
36
+ if Rails.env == "development"
37
+ zipfile.add(filename, file)
38
+ else
39
+ zipfile.add(filename.encode("Shift_JIS"), file)
40
+ end
41
+ end
42
+ end
43
+ temp_file.path
44
+ end
45
+ ```