2011-01-04 11 views
0

ファイル「Sample.bak」のようなファイルがあり、「Sample.zip」に圧縮すると、圧縮ファイルIを開いたときにZIPファイル内のファイル拡張子が失われます拡張子を付けずに「サンプル」を検索してください。vb.netを使用してファイルを圧縮するときに問題が発生する

Dim name As String = Path.GetFileName(filePath).Replace(".Bak", "") 
Dim source() As Byte = System.IO.File.ReadAllBytes(filePath) 
Dim compressed() As Byte = ConvertToByteArray(source) 
System.IO.File.WriteAllBytes(destination & name & ".Bak" & ".zip", compressed) 

またはこのコードを使用した:私はこのコードを使用

Public Sub cmdCompressFile(ByVal FileName As String) 

    'Stream object that reads file contents 
    Dim streamObj As Stream = New StreamReader(FileName).BaseStream 

    'Allocate space in buffer according to the length of the file read 
    Dim buffer(streamObj.Length) As Byte 

    'Fill buffer 
    streamObj.Read(buffer, 0, buffer.Length) 
    streamObj.Close() 

    'File Stream object used to change the extension of a file 
    Dim compFile As System.IO.FileStream = File.Create(Path.ChangeExtension(FileName, "zip")) 

    'GZip object that compress the file 
    Dim zipStreamObj As New GZipStream(compFile, CompressionMode.Compress) 

    'Write to the Stream object from the buffer 
    zipStreamObj.Write(buffer, 0, buffer.Length) 
    zipStreamObj.Close() 

End Sub 

を私は圧縮ファイル内のファイル拡張子を失うことなく、ファイルを圧縮する必要がありますしてください。

おかげで、

答えて

3

GZipStreamは、それはそれよりも何よりもしない、バイトのストリームを圧縮します。ストリームにファイル情報を埋め込むことはないので、どこかに埋め込む必要があります。

ファイル名を復元する最も簡単な方法は、元の名前で名前を付けることです。 Sample.bak.zip。

これでうまくいかない場合は、ファイル情報の埋め込みを行うSharpZipLibsamples here)を使用できます。

あなたが自分のファイル形式を作成してファイル情報を埋め込むことができない場合は、standard gzip format

関連する問題