Include DotNetZip Issue

Dec 18, 2008 at 2:19 AM
I'm writing a small utility and I'd like to use DotNetZip in my application, however I don't want to have to distribute a DLL with it. So I'm trying to include the src files in my project. However, when i try to build my project i get the following errors:

"'Ionic.Utils.Zip.ZipEntry.WillReadTwiceOnInflation.get' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipEntry.cs:492,13
'Ionic.Utils.Zip.ZipEntry.WillReadTwiceOnInflation.set' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipEntry.cs:493,13
'Ionic.Utils.Zip.ZipEntry.WantCompression.get' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipEntry.cs:549,13
'Ionic.Utils.Zip.ZipEntry.WantCompression.set' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipEntry.cs:550,13
'Ionic.Utils.Zip.ZipFile.WillReadTwiceOnInflation.get' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipFile.cs:635,13
'Ionic.Utils.Zip.ZipFile.WillReadTwiceOnInflation.set' must declare a body because it is not marked abstract or extern (CS0501) - C:\Documents and Settings\User\My Documents\Projects\ZipIt\DotNetZip\ZipFile.cs:636,13
"

What am I doing wrong or missing?

Also, I can't seem to find it, but DotNetZip is compatible with .Net 2.0 correct?

Thanks!
Coordinator
Dec 18, 2008 at 4:00 PM
re: DotNetZip and .NET 2.0 - this is in the FAQ in the front matter of this codeplex project.  DotNetZip runs on systems with .NET 2.0;  DotNetZip requires .NET 3.5 (C# 3.0) to compile. 

Are you able to compile the DotNetZip source distribution as it is?

An alternative to embedding the source of DotNetZip into your app is to embed the existing DLL as a resource, and then at runtime, extract the resource and call into the APIs.  This is exactly what the Self Extractor sample applications do.  The code in your app would look like this: 


    public class SelfExtractor
    {
        const string DllResourceName = "Ionic.Utils.Zip.dll";
        // ctor
        public SelfExtractor() { }

        static SelfExtractor()
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Resolver);
        }

        static System.Reflection.Assembly Resolver(object sender, ResolveEventArgs args)
        {
            Assembly a1 = Assembly.GetExecutingAssembly();
            Assembly a2 = null;

            Stream s = a1.GetManifestResourceStream("Ionic.Utils.Zip.dll");
            int n = 0;
            int totalBytesRead = 0;
            byte[] bytes = new byte[1024];
            do
            {
                n = s.Read(bytes, 0, bytes.Length);
                totalBytesRead += n;
            }
            while (n > 0);

            byte[] block = new byte[totalBytesRead];
            s.Seek(0, System.IO.SeekOrigin.Begin);
            s.Read(block, 0, block.Length);

            a2 = Assembly.Load(block);

            return a2;
        }

And then, to use the DotNetZip library, just make the calls as normal.
In the Visual Studio project, you need to add the DotNetZip DLL as a reference, but because you will embed the DLL into the exe at compile time, you won't distribute the DLL independently when you ship your application.
On the other hand, what you are doing - embedding the source - is a reasonable thing, and lots of people do it.
Dec 18, 2008 at 7:03 PM
Edited Dec 18, 2008 at 7:06 PM
Ah yes, I see the FAQ now, sorry about that. I just tried to compile the library in VS2008 with a target of .net 2.0 and it compile fine. Could I not add the source files to my own project and do the same thing?

Also, I'm not sure I understand how to implement the self extractor but I'd rather include the source.

Edit: I believe I see where the problem was. I was oringinally trying to use this library with SharpDevelop but now that I'm using VS2008, I do not get the same error.
Coordinator
Dec 19, 2008 at 4:39 AM
I'm not suggesting you implement a self-extractor.  I am saying that you could take a similar approach.
The self-extractor is an EXE (either a winforms or a console app) which has an embedded resource.  The resource is the DLL. 
At runtime, the code in the exe gets the embedded resource and loads the DotNetZip assembly from it. 
Then the app can call into the DLL.   The code I gave you above is the stuff that gets the resource and loads the assembly.

The result is that there is a single EXE to distribute, but no source code to maintain.  You embed the DLL into your EXE.

Not sure why you would rather include the source for DotNetZip into your project - it seems like generally the wrong thing to do, although as I said, in some cases it is reasonable.

Anyway, I'm glad you got past the error.