Hi,
I would like to Archieve files for last three months in corresponding subfolders in a directory with no thrird party dlls.And they have to zip to the specific folder in their each subfolder with format name of the folder-yyyy/mm//dd.
Please help me to code for this.
Thanks
Loading
Hemant SrivastavaPosted May 21, 2013, 10:14 AM
It takes all the files in a directory (for which you passed the path)
It iterates one by one file and compress them using GZipStream C#.Net class and saves the zipped files into a corresponding new sub-folder. The name of sub-folder is the Last Access Time of the file.
It means, suppose you have three files in a folder C:\MyDocument
AA.txt Jan 23, 2012
BB.doc Feb 4, 2013
CC.xls Jan 23, 2012
After running this code, it will create two sub-folders named '2012-01-23' and '2013-04-2013'
whereas sub-folder '2012-01-23' will contain compressed files AA.zip, CC.zip
sub-folder '2012-01-23' will contain compressed file BB.zip
Is it not what you want?
R RavulaPosted May 21, 2013, 10:34 AM
R RavulaPosted May 21, 2013, 8:18 AM
Hemant SrivastavaPosted May 20, 2013, 6:13 PM
GZipStream is one of the built-in .Net class, so you could easily compress and de-compress any file.
Hope it will help.
Hemant SrivastavaPosted May 20, 2013, 6:06 PM
Try the following class, here you need to send the directory path in CompressFiles() method:
class FileZipper
{
public void CompressFiles(string directoryPath)
{
DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);
foreach (FileInfo fileToCompress in directorySelected.GetFiles())
{
DateTime fileCreationTime = fileToCompress.LastWriteTime;
string DirectName = fileCreationTime.Year + "-" + fileCreationTime.Month + "-" + fileCreationTime.Day;
DirectName = directorySelected + "\\" + DirectName;
Directory.CreateDirectory(DirectName);
Compress(fileToCompress, DirectName);
}
}
public void Compress(FileInfo fileToCompress, string newDirectoryName)
{
using (FileStream originalFileStream = fileToCompress.OpenRead())
{
if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
{
using (FileStream compressedFileStream = File.Create(newDirectoryName + "\\" + fileToCompress.Name + ".gz"))
{
using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
{
originalFileStream.CopyTo(compressionStream);
Console.WriteLine("Compressed {0} from {1} to {2} bytes.",
fileToCompress.Name, fileToCompress.Length.ToString(), compressedFileStream.Length.ToString());
}
}
}
}
}
}