How to compress a file(s)?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I want to achive 2 things.
First I would like to compress an existing file on my harddrive.
I can easily find out if a file is compressed or not by using
"File.GetAttributes".
But no matter what I do I simply can't set the compressed attribute using
"SetAttributes".
I have no problem setting the attributes "Read-Only", "Archive" or "Hidden"
using "SetAttributes"!

How can I do this?

Secondly I would like to be able to create a "compressed folder" (as it is
called in Windows Explorer) and copy files and folders into it.

How can I do that?

Regards
Wayne
 
Wayne,

From the documentation for the SetAttributesMethod on the File class:

It is not possible to change the compression status of a File object using
the SetAttributes method.

By a compressed folder, do you mean a zip file? If that is the case,
then take a look at the System.IO.Compression namespace.

Hope this helps.
 
Hi Nicholas

Thank you for your reply.

Yes, I was refering to a .zip archive.
On your recommendation I will check out System.IO.Compression namespace.

Regarding the attributes...
What I want to do is to compress a file in the same way as if I was using
Windows Explorer, right-clicked a file, clicked Advanced and then checked the
"Compress contents to save diskspace".

Isn't there really any way to achive this using C#?

Thanks for you time and input.
Wayne

Nicholas Paldino said:
Wayne,

From the documentation for the SetAttributesMethod on the File class:

It is not possible to change the compression status of a File object using
the SetAttributes method.

By a compressed folder, do you mean a zip file? If that is the case,
then take a look at the System.IO.Compression namespace.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Wayne Gore said:
Hi

I want to achive 2 things.
First I would like to compress an existing file on my harddrive.
I can easily find out if a file is compressed or not by using
"File.GetAttributes".
But no matter what I do I simply can't set the compressed attribute using
"SetAttributes".
I have no problem setting the attributes "Read-Only", "Archive" or
"Hidden"
using "SetAttributes"!

How can I do this?

Secondly I would like to be able to create a "compressed folder" (as it is
called in Windows Explorer) and copy files and folders into it.

How can I do that?

Regards
Wayne
 
Hi Nicholas

Thank you for your reply.

Yes, I was refering to a .zip archive.
On your recommendation I will check out System.IO.Compression namespace.

Regarding the attributes...
What I want to do is to compress a file in the same way as if I was using
Windows Explorer, right-clicked a file, clicked Advanced and then checked the
"Compress contents to save diskspace".

Isn't there really any way to achive this using C#?

Thanks for you time and input.
Wayne

:








- Show quoted text -

Hi,

try this http://www.icsharpcode.net/OpenSource/SharpZipLib/
 
That will create a zip file - it won't change the file to be
compressed at the Windows file-system level.

Well, creating a zip file was one of the things he was trying to do. :)
 
Thanks for you input guys.

To be able to create a zip archive is very useful and I'm greatful for the
info.

I would really like to find out a way to compress it as if I was doing it
using Windows Explorer.
I just can't believe there is no way of doing it.
I know how I can do it using WMI but it shouldn't be necessary...

Cheers
Wayne
 
Back
Top