none
Compressing a file using standard Windows compression in C#

    Question

  • Hi
     
    We regularly compress files using the standard Windows compression feature, rather than Winzip or another compression type.  This is because we frequently send the files out to customers, who may or may have Winzip, but we know they will have Windows.

    How can I compress a file using the standard Windows compression?

    TIA
    Tuesday, February 03, 2009 10:47 AM

Answers

  • This uses the Windows compression mechanism.  Reference Microsoft Shell Controls and Automation on the Com tab.

      private void Button1_Click(object sender, System.EventArgs e)  
      {  
        string ZipFile = "D:\\C-ProgramFiles.zip";  
        System.IO.File.WriteAllText(ZipFile, "PK\x05\x06" + new String('\0', 18));  
        CopyDirectory("C:\\Program Files", ZipFile, 0);  
      }  
      public void CopyDirectory(string SourceDirectory, string DestinationDirectory, int Flags)  
      {  
        Shell32.Shell Sh = new Shell32.Shell();  
        Shell32.Folder SD = Sh.NameSpace(SourceDirectory);  
        Shell32.Folder DD = Sh.NameSpace(DestinationDirectory);  
        DD.CopyHere(SD, Flags);  
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Sh);  
      } 


    The SourceDirectory can be a directory or a file.  Look at CopyHere in help for the Flags.
    • Marked as answer by DominicB Tuesday, February 03, 2009 3:40 PM
    Tuesday, February 03, 2009 3:14 PM

All replies

  • I'm not sure which you mean by "standard Windows compression feature"; Windows supports both single-file compression (the "Compress contents" advanced attribute) and zip files (which you can explore/search into). Zip files are readable by modern Windows systems.

    The reason I ask is this: if you use the single-file compression attribute, the file will be uncompressed automatically when sent over email or put on a USB/CD. So you may not be using compression when you think you are.

            -Steve

    Tuesday, February 03, 2009 11:38 AM
  • Hi Steve

    What I mean is when you compress a file using Windows's own compression.  Right click and select Send To >> Compressed (zipped) folder.

    How can I achieve the same compression in a C# application?

    Also, we frequently encrypt these files as they are sent out via email, so seeing how this can be done would be useful too.

    TIA
    Tuesday, February 03, 2009 12:17 PM
  • This uses the Windows compression mechanism.  Reference Microsoft Shell Controls and Automation on the Com tab.

      private void Button1_Click(object sender, System.EventArgs e)  
      {  
        string ZipFile = "D:\\C-ProgramFiles.zip";  
        System.IO.File.WriteAllText(ZipFile, "PK\x05\x06" + new String('\0', 18));  
        CopyDirectory("C:\\Program Files", ZipFile, 0);  
      }  
      public void CopyDirectory(string SourceDirectory, string DestinationDirectory, int Flags)  
      {  
        Shell32.Shell Sh = new Shell32.Shell();  
        Shell32.Folder SD = Sh.NameSpace(SourceDirectory);  
        Shell32.Folder DD = Sh.NameSpace(DestinationDirectory);  
        DD.CopyHere(SD, Flags);  
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Sh);  
      } 


    The SourceDirectory can be a directory or a file.  Look at CopyHere in help for the Flags.
    • Marked as answer by DominicB Tuesday, February 03, 2009 3:40 PM
    Tuesday, February 03, 2009 3:14 PM
  • What if you don't want all the files in the directory to be zipped or what if you want the zip file to be saved in the same directory as the files being zipped?  I am looking for a method that takes a list of specified file names (strings) and a preferred zip file name.
    Nick Leuchtenberg - Software Developer, Morton Buildings, Inc.

    Put the zip file whereever you wish and put whatever files you wish in it.
    Thursday, February 11, 2010 4:17 AM