读书人

文件压缩 GZipStream ,解压后文件扩展

发布时间: 2012-06-09 17:16:42 作者: rapoo

文件压缩 GZipStream ,解压后文件扩展名丢失
我想压缩文件 比如 Text.txt . 我想压缩后得到Text.zip 文件,解压后可以得到Text.txt 文件,可是我怎么一直得到Text 文件没有扩展名.txt .
下面是我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim openFileDialog1 As New OpenFileDialog()
'openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "All files (*.*)|*.*"
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

Dim fileContents As Byte()
fileContents = My.Computer.FileSystem.ReadAllBytes(openFileDialog1.FileName)

Dim filename As String = openFileDialog1.FileName '& ".zip"
filename = filename.Substring(0, filename.LastIndexOf("."))
filename = filename & ".zip"
If File.Exists(filename) Then File.Delete(filename)
Dim fs As FileStream = New FileStream(filename, FileMode.CreateNew, FileAccess.Write)

Dim compressed_Data As Byte()

compressed_Data = Compress(fileContents)

If compressed_Data IsNot Nothing Then
fs.Write(compressed_Data, 0, compressed_Data.Length)
fs.Close()
End If
End If
End Sub

Public Function Compress(ByVal data() As Byte) As Byte()

Try
Dim ms As New System.IO.MemoryStream()
Dim zipStream As Stream = Nothing
zipStream = New GZipStream(ms, CompressionMode.Compress, True)

zipStream.Write(data, 0, data.Length)
zipStream.Close()

ms.Position = 0
Dim c_data(ms.Length - 1) As Byte
ms.Read(c_data, 0, ms.Length)
Return c_data

Catch ex As Exception

End Try

End Function


[解决办法]
GZipStream没用过,我用的unzip没有发现这样问题,建议换为Unzip吧
[解决办法]

C# code
    /// <summary>    /// 压缩文件    /// </summary>    /// <param name="filepath"></param>    public static void CreateZipFile(string filepath)    {        using (FileStream fs = new FileStream(filepath,FileMode.Open))        {            byte[] bytes = new byte[fs.Length];            fs.Read(bytes, 0, bytes.Length);                        using (FileStream outfile = File.Create(Path.ChangeExtension(filepath, "zip")))            {                using (GZipStream zipStream = new GZipStream(outfile, CompressionMode.Compress))                {                    zipStream.Write(bytes, 0, bytes.Length);                }                        }        }        }    /// <summary>    /// 解压缩文件    /// </summary>    /// <param name="filepath"></param>    public static void DeCompressZipFile(string filepath,string extension)    {        using (FileStream infile = File.OpenRead(filepath))        {            using (DeflateStream deflateStream = new DeflateStream(infile, CompressionMode.Decompress))            {                byte[] buffer = new byte[infile.Length + 100];                int offset = 0;                int totalCount = 0;                while (true)                {                    int bytesRead = deflateStream.Read(buffer, offset, 100);                    if (bytesRead == 0)                    {                        break;                    }                    offset += bytesRead;                    totalCount += bytesRead;                }                using (FileStream outfile = File.Create(Path.ChangeExtension(filepath, extension)))                {                    outfile.Write(buffer, 0, buffer.Length);                }            }        }    } 

读书人网 >VB Dotnet

热点推荐