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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification