public static string SaveFileToDiskIfGdb(HttpPostedFileBase httpPostedFileBase, GisUploadAttempt gisUploadAttempt) { GisUploadAttemptStaging.SetupDirectory(gisUploadAttempt); var fileEnding = Path.GetFileName(httpPostedFileBase.FileName); var fullFilePath = Path.Combine(GisUploadAttemptStaging.GisUploadAttemptDirectory(gisUploadAttempt), fileEnding); httpPostedFileBase.SaveAs(fullFilePath); return(fullFilePath); }
public static string UnzipAndSaveFileToDiskIfShapefile(HttpPostedFileBase httpPostedFileBase, GisUploadAttempt gisUploadAttempt, ref bool shapeFileSuccessfullyExtractedToDisk) { ZipFile zipFile = null; var shapeFilePath = string.Empty; var zipFailure = false; try { zipFile = new ZipFile(httpPostedFileBase.InputStream); } catch (Exception) { zipFailure = true; } if (!zipFailure && zipFile != null) { GisUploadAttemptStaging.SetupDirectory(gisUploadAttempt); var extensionsFound = new List <string>(); var shapeFilePathCreated = false; foreach (ZipEntry zipEntry in zipFile) { if (!zipEntry.IsFile) { continue; } var extension = Path.GetExtension(zipEntry.Name); if (ValidExtensions.Any(e => e == extension)) { if (extensionsFound.All(e => e != extension)) { extensionsFound.Add(extension); } var shapefileNameWithExtension = Path.GetFileName(zipEntry.Name); if (shapefileNameWithExtension == null) { continue; } // this file is a "keeper", extract it and write it to disk var fullFilePath = Path.Combine(GisUploadAttemptStaging.GisUploadAttemptDirectory(gisUploadAttempt), shapefileNameWithExtension); if (extension.Equals(".shp")) { shapeFilePath = fullFilePath; shapeFilePathCreated = true; } var buffer = new byte[4096]; // 4K is optimum var zipStream = zipFile.GetInputStream(zipEntry); // unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size of the file, but does not waste memory using (var streamWriter = System.IO.File.Create(fullFilePath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } shapeFileSuccessfullyExtractedToDisk = extensionsFound.Count == ValidExtensions.Count && shapeFilePathCreated; } return(shapeFilePath); }