使用 vb.net 压缩文件时出现问题
我有像“Sample.bak”这样的文件,当我将其压缩为“Sample.zip”时,我丢失了zip文件内的文件扩展名,我的意思是当我打开压缩文件时,我发现“Sample”没有任何扩展名。
我使用此代码:
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
请我需要压缩文件而不丢失压缩文件内的文件扩展名。
谢谢,
I have File Like "Sample.bak" and when I compress it to be "Sample.zip" I lose the file extension inside the zip file I meann when I open the compressed file I find "Sample" without any extension.
I use this code :
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)
Or using this code :
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
please I need to compress the file without loosing file extension inside compressed file.
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GZipStream 压缩字节流,仅此而已。它不会在流中嵌入文件信息,因此您需要将其嵌入某处。
恢复文件名的最简单方法是将其命名为原始名称,例如 Sample.bak.zip。
如果这不起作用,您可以使用 SharpZipLib (示例)它为您嵌入文件信息。
如果您不想采用任何一种方法,您可以创建自己的文件格式并嵌入文件信息,或者您可以尝试实现 标准 gzip 格式
GZipStream compresses a stream of bytes, it does no more than that. It does not embed file info in the stream, so you need to embed it somewhere.
The easiest way to restore the file name is to name it with the original name e.g. Sample.bak.zip.
If that doesn't work for you can use SharpZipLib (samples here) which does the embedding of the file info for you.
If you don't want to go that either of thoes you can either create your own file format and embed the file info or you can try and implement the standard gzip format