0

私はこの質問のポスターとまったく同じ問題を抱えています: Ruby (Errno::EACCES) on File.delete。彼とは異なり、彼のソリューションで提供された変更は私には機能しません。

これが私のコードです。これは、元のファイルを削除したい圧縮アルゴリズムです。

uncompressed_file = File.new(Rails.root + filepath)
compressed_file = File.new(Rails.root + "#{filepath[0..filepath.size - 1]}.gz", "w+b")
file_writer = Zlib::GzipWriter.new(compressed_file)

buf = ""
File.open(uncompressed_file, "rb") do | uncompressed |
  while uncompressed.read(4096, buf)
    file_writer << buf
  end
  file_writer.close
end    

begin      
  files_changed_by_chmod = File.chmod(0777, uncompressed_file)
rescue
  puts "Something happened"
end    
puts "Number of files changed by CHMOD : " + files_changed_by_chmod.to_s
File.delete(uncompressed_file)
File.rename(Rails.root + "#{filepath[0..filepath.size - 1]}.gz", Rails.root + filepath)

putschmod で何が起こっているかを確認するために、そこにいくつかあることに気付くでしょう。出力は次のとおりです。

Number of files changed by CHMOD : 1

ありませんSomething happened。したがって、chmod を実行してもエラーは発生せず、chmod は実際に 1 つのファイル (おそらくuncompressed_file.) を変更しますが、それでも削除行で Errno::EACCESS エラーが発生します。

ファイルを削除できないのはなぜですか?! それは私を壁に追いやっています。Windows 8 と ruby​​ 1.9.3 を実行しています。

編集:以下の最初の回答は、ファイルを削除できないという問題を解決します。ただし、コードが実行しようとしているジョブは無効になります (つまり、ソリューションで提供されている圧縮アルゴリズムを使用してファイルを実行し、次に他のアルゴリズムを実行すると、ファイルが破損して返されます)。はい、インフレーション メソッドでコーディング スタイルをエミュレートしようとしましたが、役に立ちませんでした。ファイルの暗号化、復号化、解凍を実行する残りのコードは次のとおりです。

def inflate_attachment(filepath)
    compressed_file = File.new(Rails.root + filepath)

    File.open(compressed_file, "rb") do | compressed |
      File.open(Rails.root + "#{filepath[0..filepath.size - 7]}_FULL.enc", 'w+b') do | decompressed |
        gz = Zlib::GzipReader.new(compressed)
        result = gz.read
        decompressed.write(result)
        gz.close
      end
    end
  end

def encrypt_attachment(filepath, cipher)
    unencrypted_file = File.new(Rails.root + filepath)     
    encrypted_file = File.new(Rails.root + "#{filepath[0..filepath.size - 1]}.enc", "w")

    buf = ""
    File.open(encrypted_file, "wb") do |outf|
      File.open(unencrypted_file, "rb") do |inf|
        while inf.read(4096, buf)
          outf << cipher.update(buf)
        end
        outf << cipher.final
      end
    end
  end

def decrypt_attachment(filepath, key, iv)
    cipher = OpenSSL::Cipher.new(ENCRYPTION_TYPE)
    cipher.decrypt
    cipher.key = key
    cipher.iv = iv

    encrypted_file = File.new(Rails.root + filepath)  
    decrypted_file = File.new(Rails.root + "#{filepath[0..filepath.size - 5]}.dec", "w")

    buf = ""
    File.open(decrypted_file, "wb") do |outf|
      File.open(encrypted_file, "rb") do |inf|
        while inf.read(4096, buf)
          outf << cipher.update(buf)
        end
        outf << cipher.final
      end
    end
  end
4

1 に答える 1

0

ファイルを適切に閉じていないことが原因である可能性があると思います。私は自由にあなたのコードを書き直しましたが、chmod は必要ありませんでした。

filename = <your sourcefilename goes here>
filename_gz = filename + ".gz"

filepath = Rails.root + filename
filepath_gz = Rails.root + filename_gz

# gzip the file
buffer = ""
File.open(filepath) do |file|
  Zlib::GzipWriter.open(filepath_gz) do |gz|
    while file.read(4096, buffer)
      gz << buffer
    end
  end
end

# moves the filepath_gz to filepath (overwriting the original file in the process!)
FileUtils.mv(filepath_gz, filepath)

ご覧のとおり、File.open(path) を使用してブロックを渡しました。これにより、ブロックが終了するとファイルが自動的に閉じられます。また、削除/名前変更コードを変更して、gzip ファイルを元のパスに移動するだけで、同じ効果があります。

ただし、元のファイルのバックアップを取っておくことを強くお勧めします。

于 2013-04-15T16:54:49.063 に答える