How to do Split and Zip files using Xceed Software
If you have a requirement as below
1) If file size > 10 MB then first Zip the file then split into selected size (Ex: 10MB, 20MB 50Mb)
And give option to download those file one by one.
2) If file size is < 10MB then Zip the file and popup download dialog box.
You can use Xceed Component for this kind of requirement (www.xceed.com).
This will provide XceedZipLib.dll with License key you can use that in your C#.net code.
Overview
Xceed component will give you spitted files in the .Z01 extension.
For example if your file size is 30 MB and you want to split into 10 MB then this will give you 3 files
1) Filename.Z02 2) FileName.Z03 3) FileName.zip
After this you need to rename .Zip to .Z01 and Last file .Z03 to .zip.
Now you have file 1) Filename.Z01 2) FileName.Z02 3) FileName.zip (Save into application server)
Users can download these three files in (all files must be in one location/folder) their location and
Using WinZip 8.0 and above they can unzip FileName.zip file.If you unzip this it will merge all files into one
User can see these files to download one by one as below
Filename.Z01
Filename.Z02
Filename. zip
Source code in C#.Net
Add Reference
XceedZipLib.dll
Name Space
using XceedZipLib;
Variable Declaration
private string license_Key = "1234567789999"; (Get license from Xceed software)
private string destination_Folder = "C:\backup\";
private xcdError retVal;
public const int FILE_SIZE_MB = 1048576;
Methods
public xcdError ZipFile(string filePathToZip, int fileSize, string zipFileName, string sessionId)
{
XceedZipClass objZip = new XceedZipClass();
//If destination folder not present then create
if (!System.IO.Directory.Exists(destination_Folder))
{
System.IO.Directory.CreateDirectory(destination_Folder);
}
zipFileName = destination_Folder + "\\" + sessionId + "_" + zipFileName + ".zip";
try
{
bool isLincenseVersion = objZip.License(license_Key);
if (isLincenseVersion)
{
objZip.SplitSize = fileSize * Convert.ToInt32(Constant.FILE_SIZE_MB);
objZip.SpanMultipleDisks = xcdDiskSpanning.xdsNever;
objZip.PreservePaths = true;
objZip.ZipFilename = zipFileName;
objZip.BasePath = GetFolderName(filePathToZip);
objZip.FilesToProcess = GetFileName(filePathToZip);
objZip.ProcessSubfolders = true;
retVal = objZip.Zip();
if (retVal == xcdError.xerSuccess)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(destination_Folder);
FileInfo[] files = dir.GetFiles(sessionId + "*");
FileInfo[] zipFile = dir.GetFiles(sessionId + "*.zip");
if (files.Length > 1)
{
//Rename the files (Zip to .Z01 and Last file to .zip)
string finalFile = files[files.Length - 2].ToString();
File.Move(destination_Folder + zipFile[0].ToString(),
destination_Folder + zipFile[0].ToString().Substring(0,
zipFile[0].ToString().LastIndexOf(".")) + ".Z01");
File.Move(destination_Folder + finalFile,
destination_Folder + finalFile.Substring(0,
files[files.Length - 1].ToString().LastIndexOf(".")) + ".zip");
}
return retVal;
}
else if (retVal == xcdError.xerCannotUpdateAndSpan)
{
//File already present in target folder
lblError.Text = Constant.FILE_ALREADY_EXIST;
}
else if (retVal == xcdError.xerWarnings)
{
//File split but may not merge please download again.
lblError.Text = Constant.FILE_SPLIT_WARNING;
}
}
else
{
return xcdError.xerNotLicensed;
}
}
catch (Exception Ex)
{
throw Ex;
}
return retVal;
}
private static string GetFileName(string filePath)
{
return filePath.Substring(filePath.LastIndexOf("\\") + 1);
}
private static string GetFolderName(string filePath)
{
return filePath.Substring(0, filePath.LastIndexOf("\\"));
}
/// This method invoks from global.asax when session_end method.
/// This will delete all created files for download.
public void DeleteFile(string sessionId)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(destination_Folder);
FileInfo[] files = dir.GetFiles(sessionId + "*");
for (int fileCount = 0; fileCount < files.Length; fileCount++)
{
File.Delete(destination_Folder + files[fileCount]);
}
}
Example to call this method
xcdError retval = ZipFile(@"C:\backup\std.pdf",10,"DocName",Session.SessionId)