Page 1 of 1
Data Exchange Modifications (Alternative to ZIP Files)
Posted: Mon Aug 05, 2013 11:42 am
by lquessenberry
I am wondering if anyone has come up with a way to forego the ZIP container around the CSV or XML files in favor of providing and export in CSV or XML without it being contained in a ZIP file. I would love to not have to uncompress the XML files and access them directly.
Ideally, I would like to be able to modify the OrderExport functionality to perhaps write a duplicate XML file into a separate directory with a static filename that is overwritten each time that an order export is executed so I would have a constant URL with updated data at each time an export is made.
Re: Data Exchange Modifications (Alternative to ZIP Files)
Posted: Mon Aug 05, 2013 12:23 pm
by Naveed
You can write a generic ASHX handler for a static URL. That generic ashx handler can scan a directory ( data exchange download folder) and detect the latest exported order file (compressed), un-compress it into memory or to a temporary location and return the contents to you.
Re: Data Exchange Modifications (Alternative to ZIP Files)
Posted: Mon Aug 05, 2013 12:47 pm
by lquessenberry
That sounds awesome, but here's the catch. I am not familiar with ASHX, but I understand your logic here. Do you know of any particular examples of code that I can look at for this? I am used to PHP development and I am not a complete dummy when it comes to code, but knowing where to start would be awesome. Any ideas?
Re: Data Exchange Modifications (Alternative to ZIP Files)
Posted: Wed Aug 07, 2013 2:04 am
by Naveed
I try to explain some related code stuff:
1. You can locate the latest modified file using some code below:
Add following namespaces with using statements:
Code: Select all
using System.Collections.Generic;
using System.IO;
using CommerceBuilder.Common;
Code: Select all
string mappedPath = Server.MapPath("~/App_Data/DataExchange/Download/");
if (Directory.Exists(mappedPath))
{
string fileTypes = "ORDERS_*.zip";
string[] backupFilePaths = fileTypes.Split('|').SelectMany(filter => Directory.GetFiles(mappedPath, filter, SearchOption.TopDirectoryOnly)).ToArray();
if (backupFilePaths != null && backupFilePaths.Length > 0)
{
IList<FileInfo> backupFiles = new List<FileInfo>();
// GET FILE NAMES
for (int i = 0; i < backupFilePaths.Length; i++)
{
FileInfo fileInfo = new FileInfo(backupFilePaths[i]);
backupFiles.Add(fileInfo);
}
backupFiles.Sort("LastWriteTime", CommerceBuilder.Common.SortDirection.DESC);
// LAST MODIFIED FILE
FileInfo lastFile = backupFiles[0];
}
}
Here is how you can uncompress the contents of zip file:
Add using statement at the top:
Code: Select all
using ICSharpCode.SharpZipLib.Zip;
Uncompress to a directory:
Code: Select all
// UNCOMPRESS LAST FILE
// EXTRACT TO A TEMPORARY DIRECTORY, WHICH WILL BE DELETED AT THE END
string extractDirectory = "~/App_Data/DataExchange/Download/" + Guid.NewGuid().ToString() + "/";
try
{
FastZip fastZip = new FastZip();
fastZip.ExtractZip(filePath, SafeMapPath(extractDirectory), string.Empty);
// LOCATE THE UNCOMPRESSED FILE, AND PROCESS THE FILE CONTENTS
}
finally
{
// DELETE THE TEMP DIRECTORY
Directory.Delete(SafeMapPath(extractDirectory), true);
}
I hope it will help.
Re: Data Exchange Modifications (Alternative to ZIP Files)
Posted: Wed Aug 14, 2013 6:02 am
by lquessenberry
Thank you Naveed. This will help greatly. I will begin trying to implement some of this and give it a shot.