Reputation: 836
i am using this code for compression of file to zip
public static void Compress(FileInfo fileToCompress)
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".zip")
{
using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + "_" + DateTime.Now.ToString("dd_MM_yyyy") + ".zip"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
originalFileStream.CopyTo(compressedFileStream);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString());
}
}
}
}
}
with this code i convert file to zip for example i have file with name myfile.pdf what it does, it saves this file with myfile.pdf_31_7_2013.zip but the problem is when i extract this zip it contains my file with extention myfile.pdf_31_7_2013 which will be invaild file coz its extention changed from .pdf to .pdf_31_7_2013
so its extention is changed. i want to modify code in a way that zip name should be the same as it performs right now. but inside that my file should be with only myfile.pdf
please help me to solve this. thanks in advance
Upvotes: 1
Views: 161
Reputation: 1500515
You should change the file format so that:
gz
.gz
has been removed, it will be a reasonable filename.So instead of converting myfile.pdf
into myfile.pdf_31_7_2013.zip
, I'd convert it into myfile-20130731.pdf.gz
. When it's extract, it will become myfile-20130731.pdf
. Note that using yyyyMMdd
is cleaner than MM_d_yyyy
as it's sortable and unambiguous.
Upvotes: 2