locked
ZIP a file using VB.net RRS feed

  • Question

  • User-54634383 posted

    I'm currently sitting with a solution that requires 'Zipping' a file. There are many solutions using Java or C# code, but none I can implement using the server I'm working with so it needs to be done in VB.

     

    Any answers will be greatly appriciated.

    Thursday, August 12, 2010 7:06 AM

Answers

All replies

  • User-815382200 posted

    Hi,

    VB and C# are equivalent, everything that can be written in c# can be written in VB too. Good news is also that you can directly convert C# to VB with this tool: http://www.developerfusion.com/tools/convert/csharp-to-vb/

     

    Here is a simple method that compress a file and use no external libraries:

    public static void GZipCompression(string input, string output)
            {
                FileStream fs = new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] data = new byte[fs.Length];
                fs.Read(data, 0, data.Length);
                fs.Close();
    
                FileStream compressed = new FileStream(output, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                GZipStream gzip = new GZipStream(compressed, CompressionMode.Compress, false);
                gzip.Write(data, 0, data.Length);
                gzip.Close();
                compressed.Close();
            }


    Just pass correct paths using Server.MapPath().

    Hope it helps...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:36 AM
  • User197322208 posted

    You can use this component (source in C#, but you can add as a reference the dll)

    http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx


    or , for native GZip, you can use

    http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:38 AM
  • User-731863672 posted

    If you reference a binary .NET library, it does not matter whether it has been compiled from C#, VB or another .NET language.

    For example our component Rebex.ZIP is written in C#, but all samples and tutorials are published both in C# and VB.NET.
    Your question - zipping some files, would be solved by following two lines of code:
    (taken from tutorial page http://www.rebex.net/zip.net/tutorial-zip.aspx)

     
    Imports Rebex.IO.Compression
    ' add all *.TXT files from the local directory C:\Data\
      ' to the directory \Data-2010 (within the Zip file)  ' (ZIP archive C:\archive.zip doesn't have to exist)  ZipArchive.Add("C:\archive.zip", "C:\Data\*.txt", "\Data-2010")

    The code need to reference .DLL which can be downloaded here.

    Friday, September 10, 2010 9:01 AM