CodeCloudy

Azure | .Net | JQuery | Javascript | Umbraco

Simplest way of compressing files using .net 4.5

on April 1, 2014

 

We will try to create a sample project to demonstrate compression using .net 4.5

  1. Create a sample Console project
  2. Right click references > Add References

    Search and add the DLL “System.IO.Compression.FileSystem”

     

  3. Now let’s add a sample folder called “test” and add 2 sample text files in it.

  4. Now select the 2 sample test files and change the property “Copy to output directory” to “copy if newer”

    This will create the test folder and copy both the test files in to it in the debug folder which is the working folder for the program.

     

  5. Now you can use the following code in the Main program to compress the “test” folder

     

    Syntax (C#)

    public static void CreateFromDirectory(

        string sourceDirectoryName,

        string destinationArchiveFileName

    )

    sourceDirectoryName: The path to the directory to be archived, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

    destinationArchiveFileName: The path of the archive to be created, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.

     

     

     

    System.IO.Compression.ZipFile.CreateFromDirectory(“test”, “testzip.zip”);

     

    Output in Debug folder:

     

    Applying this in Windows Azure:

    Storing in the local file system won’t be applicable in azure environment. We need to store in a centralized location where all servers can access them.

    You can upload your files into a blob container and compress those files using a worker role & store them in another blob container.

     

    Moreover, there are plenty of other libraries that we use to compress. All has its pros and cons.

     

    Download Code Sample from HERE

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: