Search:
Namespaces
Discussions
.NET v1.1
Feedback
Is it possible to embed a zipped text file, then unzip or read from it at runtime?
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Yogi_Bear_79
pardon my ignorance as I am a self-taught hobbyist programmer.
I am curious after reading up on SharpZipLib. Can I embed a zipped txt file
in my program? Then either read from within the zip file or unzip and read
it? I currently have an embedded text file that contains a list that is
read into an array. I'm always looking to save space. And I could reduce my
file size 75% if it was zipped! I have looked at the SharpZipLib web site,
downloaded their examples. I don't see any that demonstrate this. They
mainly seem to demonstrate zipping/unzipping files/folders on the local
drives.
Thanks in advance :)
Reply to this message...
typeMisMatch
Hi,
You can embed your zipped file as binary data in a resource file. Extract
this data into a Stream and
pass it to the SharpZipLib as if it was a file stream. This should work
fine.
-c
http://www.typemismatch.com/
"Yogi_Bear_79" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Yogi_Bear_79
Any chance you coud give an example of how to do this? Currently the file
is a .txt Are yousaying convert it to binary, then zip it. THen use
SharpZipLib to unzip it at run time to read it's contents? All the while
keeping everything embedded so my .exe is stand alone?
"typeMisMatch" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Jon Skeet [C# MVP] (VIP)
Yogi_Bear_79 <
Click here to reveal e-mail address
> wrote:
[Original message clipped]
No, just zip it. In fact, gzip it instead - there's no need to have a
zip file containing a single embedded file when you can just have the
gzip file and not worry about ZipEntry instances etc.
> THen use SharpZipLib to unzip it at run time to read it's contents?
Yes.
> All the while keeping everything embedded so my .exe is stand alone?
Yes.
--
Jon Skeet - <
Click here to reveal e-mail address
>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
Yogi_Bear_79
Jon,
Ok I guess I need some hints. All I have gotten accomplished so far is using
gzip to zip the file. I now have an emedded file. Previously I had an
embedded .txt file that I read into an
ArrayList
...Code snippet below. I've
reviewed the samples and searched the net and I'm not sure where to go from
here. Does the file get unzipped to a temp hard drive location then read
with the below code, or is unzipped and read into the
ArrayList
all from
memory?
private void AddRestrictedSites()
{
using (Stream
stream=GetType().Assembly.GetManifestResourceStream("Build_Script.Sites.txt"
))
{
using (
StreamReader
reader = new
StreamReader
(stream))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
resourceSites.Add(line);
}
}
}
"Jon Skeet [C# MVP]" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Jon Skeet [C# MVP] (VIP)
Yogi_Bear_79 <
Click here to reveal e-mail address
> wrote:
[Original message clipped]
It's all done in memory. All you need is a single extra using block
between fetching the stream and passing it to a reader:
private void AddRestrictedSites()
{
using (Stream stream=GetType().Assembly.GetManifestResourceStream
("Build_Script.Sites.txt"))
{
using (Stream decompressedStream = new GzipInputStream(stream)
{
using (
StreamReader
reader = new
StreamReader
(decompressedStream))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
resourceSites.Add(line);
}
}
}
}
}
--
Jon Skeet - <
Click here to reveal e-mail address
>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
Yogi_Bear_79
I added the code to my test program. Below I added the using directives that
I have in this class as well. I checked these against the samples for
SharpZipLib and they appear correct to me. Yet I get the following error:
(31): Cannot implicitly convert type '<error>' to 'System.IDisposable'
(31): The type or namespace name 'GzipInputStream' could not be found (are
you missing a using directive or an assembly reference?)
It is erroring on the following line of code:
using (Stream decompressedStream = new GzipInputStream(stream)
Specifically it doesn't seem to knwo what to do with
GzipInputStream
[Original message clipped]
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using ICSharpCode.SharpZipLib.GZip;
private void AddRestrictedSites()
{
using (Stream
stream=GetType().Assembly.GetManifestResourceStream("Build_Script.Sites.txt"
))
{
using (Stream decompressedStream = new GzipInputStream(stream))
{
using (
StreamReader
reader = new
StreamReader
(stream))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
resourceSites.Add(line);
}
}
}
foreach(string x in resourceSites)
{
Console
.WriteLine(x);
}
}//end
Reply to this message...
Yogi_Bear_79
Getting further now. I got the program to compile without errors. I had a
letter lowercase instead of uppercase.
But now I get this error when I test run.
An unhandled exception of type 'System.ArgumentNullException' occurred in
icsharpcode.sharpziplib.dll
Additional information: Value cannot be null.
It is idicating the following line of code as the probelm point:
using (Stream decompressedStream = new GZipInputStream(stream))
From what I can tell, it seems that the program is not recognizing the .gz
file. The file is an embedded .gz file.
I ziped it with GZIP via the command line with the following syntax C:\gzip
sites
private void AddRestrictedSites()
{
using (Stream stream =
GetType().Assembly.GetManifestResourceStream("sites.gz"))
{
using (Stream decompressedStream = new GZipInputStream(stream))
{
using (
StreamReader
reader = new
StreamReader
(decompressedStream))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
resourceSites.Add(line);
}
}
}
}
foreach(string x in resourceSites)
{
Debug
.WriteLine(x);
}
}
Reply to this message...
Yogi_Bear_79
Disregard, and thanks for the support!
After stareing at it long enough I realized I pulled out the fully qualified
name for my file, thus the program couldn't find it!
"Yogi_Bear_79" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
tribal
I think you could also add your zip into your solution
and in the properties for it specify an embedded resource
You could then read the file using
ResourceManager
class
google for C#
ResourceManager
embedded and I am sure you
will get some examples
[Original message clipped]
Reply to this message...
System.ArgumentNullException
System.Collections.ArrayList
System.Console
System.Diagnostics.Debug
System.IDisposable
System.IO.StreamReader
System.Resources.ResourceManager
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com