Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All
How can i Compress Specific Files In Specific Folder into zip folderso result will be:
d:/zipped.zip
If I extract The Folder The Result will be The Same Paths Of original Folder
ex
d:/txt12.txt
d:/Folder1/txt2.txt
d:/Folder1/txt1.txt
With With Out Using DLL dependency
and this Example Using Ionic.Zip
C#
using (ZipFile zip = new ZipFile())
{
 foreach (var files in ListfileFolderPaths)
 {
  flInfo = new FileInfo(files);
  if ((flInfo.CreationTime >= from_date) || (flInfo.LastWriteTime >= from_date))
  {
   zip.AddFiles(new[] {flInfo.FullName},"directParentFolderNameOfFIleEX:NewFolder");
   zip.Save("ZipFolderPath");
  }
 }
}


Thanks
Posted
Updated 20-May-15 2:02am
v3
Comments
F-ES Sitecore 20-May-15 7:00am    
You haven't said where these files are, but if you're planning on compressing or extracting files on the client then you can't, these files and folders have to be on the server.
Member 11593359 20-May-15 7:05am    
yes it is on the server Side not in client Side

1 solution

Just read up on the zip methods:

https://msdn.microsoft.com/en-us/library/ms404280%28v=vs.110%29.aspx[^]

It's all there.

using this article:
Creating Zip Files Easily in .NET 4.5[^]

I have created this example
C#
using System;
using System.IO;
using System.IO.Compression;

...

using (ZipArchive zip = ZipFile.Open (zipName,ZipArchiveMode.Create)) 
 {
  foreach (var files in ListfileFolderPaths)
  {
   flInfo = new FileInfo(files);
   if ((flInfo.CreationTime >= from_date) || (flInfo.LastWriteTime >= from_date)) 
   {
    zip.CreateEntryFromFile(flInfo.FullName,flInfo.Name);
   }
  }
 }//The zip will be finalized on dispose


As with all my code, it will need tweaking. Hopefully this will get you 90% of the way
 
Share this answer
 
v2
Comments
Member 11593359 20-May-15 7:03am    
yes i have read it but how can i customize it to my Application Purpose
Andy Lanng 20-May-15 7:06am    
I really don't understand what you're struggling with. It seems your requirements are very basic and the first example of that page should be enough.

Show me your code and where you're stuck. I'll get a better understanding
Member 11593359 20-May-15 7:21am    
Using Ionic.Zip
using (ZipFile zip = new ZipFile()) {
foreach (var files in ListfileFolderPaths)
{
flInfo = new FileInfo(files);
if ((flInfo.CreationTime >= from_date) || (flInfo.LastWriteTime >= from_date))
{
zip.AddFiles(new[] { flInfo.FullName },"directParentFolderNameOfFIleEX:NewFolder");
zip.Save("ZipFolderPath");
}
}
}
but ineed to remove Ionic.Zip from my project
Andy Lanng 20-May-15 8:16am    
Solution updated
Member 11593359 20-May-15 9:16am    
thank you very much but it give me an error
The process cannot access the file 'D\test1\test1.zip' because it is being used by another process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900