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

Y

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 :)
 
T

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/
 
Y

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?
 
T

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
 
J

Jon Skeet [C# MVP]

Yogi_Bear_79 said:
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.
 
Y

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);
}
}
}
 
J

Jon Skeet [C# MVP]

Yogi_Bear_79 said:
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.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);
}
}
}
}
}
 
Y

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

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.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
 
Y

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);
}
}
 
Y

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top