By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,676 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,676 IT Pros & Developers. It's quick & easy.

Is it possible to embed a zipped text file, then unzip or read from it at runtime?

P: n/a
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 :)
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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" <IT*****@spamfree.com> wrote in message
news:Eq********************@comcast.com...
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 :)

Nov 16 '05 #2

P: n/a
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" <ne**@ip80.com> wrote in message
news:u9*************@TK2MSFTNGP09.phx.gbl...
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" <IT*****@spamfree.com> wrote in message
news:Eq********************@comcast.com...
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 :)


Nov 16 '05 #3

P: n/a

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-----
Any chance you coud give an example of how to do this? Currently the fileis a .txt Are yousaying convert it to binary, then zip it. THen useSharpZipLib to unzip it at run time to read it's contents? All the whilekeeping everything embedded so my .exe is stand alone?
"typeMisMatch" <ne**@ip80.com> wrote in message
news:u9*************@TK2MSFTNGP09.phx.gbl...
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" <IT*****@spamfree.com> wrote in message
news:Eq********************@comcast.com...
> 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 :)
>
>


.

Nov 16 '05 #4

P: n/a
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
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.
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

P: n/a
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.GetManifestResourceStrea m("Build_Script.Sites.txt"
))
{
using (StreamReader reader = new StreamReader(stream))
{
string line;
while ( (line=reader.ReadLine()) != null)
{
resourceSites.Add(line);
}
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
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.


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6

P: n/a
Yogi_Bear_79 <IT*****@spamfree.com> wrote:
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?


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.GetManifestResourceStrea m
("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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7

P: n/a
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

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:


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.GetManifestResourceStrea m("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
Nov 16 '05 #8

P: n/a
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

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:


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.GetManifestResourceStrea m("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
Nov 16 '05 #9

P: n/a
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("site s.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);
}
}
Nov 16 '05 #10

P: n/a
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" <IT*****@spamfree.com> wrote in message
news:6p********************@comcast.com...
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("site s.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);
}
}

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.