2

.net 4.5 TripleDES ルーチンを使用して暗号化/復号化できる小さなユーティリティがあります。圧縮を追加しましたが、単純な圧縮ではうまくいかないことがわかりました。

これらのフィールドで参照されるファイルは確かに存在し、まったく巨大ではなく、同じケースで名前が付けられています。

それでも、「ディレクトリ名が無効です。」というメッセージが表示され続けます。アーカイブの存在をテストすると、別のエラーが発生します-zip が作成され、0 バイトであるためです。昨日、ユーティリティの暗号化部分の使用方法を理解するのにかかったよりもはるかに長い間、これについて検索してきました。それは、ストリームがどのように機能するかを学ぶことです!

誰でもこの問題に光を当てることができますか?

Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"

Private Sub Main()
  Compress(Encoded, CompressEncoded)
End sub

Sub Compress(filename As String, zippedFile As String)
'Added to handle file already exists error
  If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
  If IO.File.Exists(filename) Then IO.Compression.ZipFile.CreateFromDirectory(filename, zippedFile, CompressionLevel.Fastest, True)
End Sub

このコードに加えて、ファイル名が存在することを証明するために簡単なストリームリーダー テストを追加してみました。' Dim sr As New StreamReader(filename) MessageBox.Show(sr.ReadToEnd)

その中のテキスト行を表示します。

私はどんな助けにも感謝します - このかなりばかげたバグは、私がもっと便利なことに使うことを望んでいた今日の午後の多くの時間を食いつぶしました:)。

ありがとう!

答えてくれてありがとう!

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

Sub Decompress(ZippedFile As String, UnzippedFile As String)
    If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)

    If IO.File.Exists(ZippedFile) Then
        Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
            archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
        End Using
    End If
End Sub
4

2 に答える 2

2

単一のファイルを圧縮する場合は、ZipArchive次のようなクラスを使用します。

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = ZipFile.Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

追加情報

クラスのSystem.IO.Compression.dllSystem.IO.Compression.FileSystem.dllへの参照を追加する必要があります。これらの DLL は、プロジェクトが VS で作成されるとき、デフォルトでは参照されません。ZipArchiveZipFile

于 2013-06-16T17:31:13.107 に答える