C# ZIP Compression Questions
So the program that I'm writing is going to be able to have lots of user generated content. What i would like to do is to allow users who make their own scenes to be able to distribute these in Zip files. I don't want them to be extracted to the hard drive when my program accesses them. Dose anyone know how to do this? I'm using a library called SharpZipLib and i can get elements from the zip file. however i don't know how to get it to stream uncompressed data into something that i can read. mostly I'm going to be holding XML, mesh, hlsl, and image files.
dose anyone know where i can find info on that?
Thanks!
neon
Look at the ZipInputStream and ZipOutputStream classes. They are both derived from Stream, so just use typical Stream-compatible methods to read/write them.
(Edit) They seem to work a lot like the .Net 2.0 compression streams: You put them "on top" of another stream, and they compress the data you write to them and write the result into the underlying stream.
For example, if you want to open a file and begin reading it as data in the program, you would:
- Get a FileStream by opening a file.
- Construct a ZipInputStream using the FileStream as the constructor argument.
- Use various ZipInputStream methods to find the entry you want.
- Use the ZipInputStream as a Stream and read your decompressed data.
(Edit) They seem to work a lot like the .Net 2.0 compression streams: You put them "on top" of another stream, and they compress the data you write to them and write the result into the underlying stream.
For example, if you want to open a file and begin reading it as data in the program, you would:
- Get a FileStream by opening a file.
- Construct a ZipInputStream using the FileStream as the constructor argument.
- Use various ZipInputStream methods to find the entry you want.
- Use the ZipInputStream as a Stream and read your decompressed data.
as Nypyren mentioned (here are VB.Net sample; you can change to C# easily):
compress (bytData is your data in binary format; strZipEntry is your filename):
while de-compressing should be similar to:
compress (bytData is your data in binary format; strZipEntry is your filename):
Public Function CompressWithSharpZipLib(ByVal bytData As Byte(), ByVal strZipEntry As String) As Byte() Dim streamCompressedOutput As MemoryStream = New MemoryStream() Dim s As ZipOutputStream = Nothing Try s = New ZipOutputStream(streamCompressedOutput) s.SetLevel(5) ' 0 - store only to 9 - means best compression Dim entry As ZipEntry = New ZipEntry(strZipEntry) s.PutNextEntry(entry) s.Write(bytData, 0, bytData.Length) bytData = Nothing s.Finish() s.Close() Return streamCompressedOutput.ToArray() Catch ex As Exception streamCompressedOutput = Nothing bytData = Nothing If Not s Is Nothing Then s.Finish() s.Close() End If Return Nothing End Try End Function
while de-compressing should be similar to:
Public Shared Function unPackZipFiles(ByVal file As String) As String() Dim streamWriter As FileStream = Nothing Dim sFiles As String() = Nothing Dim arr As ArrayList = Nothing Dim CS$1$0000 As String() Try If AppUtils.IsZip(file) Then Dim theEntry As ZipEntry Dim inputStream As New ZipInputStream(File.OpenRead(file)) arr = New ArrayList Do While (Not theEntry = inputStream.GetNextEntry Is Nothing) Dim dirPath As String = Path.GetDirectoryName(file) If (Path.GetFileName(theEntry.Name) <> String.Empty) Then streamWriter = File.Create((dirPath & "\" & theEntry.Name)) arr.Add(streamWriter.Name) sFiles = TryCast(arr.ToArray(GetType(String)),String()) Dim size As Integer = 0 Dim sizeOfFile As Integer = CInt(theEntry.Size) Dim data As Byte() = New Byte(sizeOfFile - 1) {} Do size = (size + inputStream.Read(data, size, (data.Length - size))) Loop While (size < sizeOfFile) inputStream.ReadByte If (data.Length > 0) Then streamWriter.Write(data, 0, data.Length) End If streamWriter.Close End If Loop inputStream.Close File.Delete(file) Return sFiles End If arr = New ArrayList arr.Add(file) sFiles = TryCast(arr.ToArray(GetType(String)),String()) CS$1$0000 = sFiles Catch ex As Exception Throw New Exception(ex.Message) End Try Return CS$1$0000End Function
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement