internal static void Extract(System.IO.Stream tarStream, Models.FileTransportInfo fileTransferInfo) { using (TarInputStream tarIn = new TarInputStream(tarStream)) { TarEntry tarEntry = null; while ((tarEntry = tarIn.GetNextEntry()) != null) { if (tarEntry.IsDirectory) { continue; } string name = tarEntry.Name.Replace('/', System.IO.Path.DirectorySeparatorChar); if (System.IO.Path.IsPathRooted(name)) { name = name.Substring(System.IO.Path.GetPathRoot(name).Length); } string outName = System.IO.Path.Combine(fileTransferInfo.DestinationFullNameWithBasePath, name); string directoryName = System.IO.Path.GetDirectoryName(outName); System.IO.Directory.CreateDirectory(directoryName); System.IO.FileStream outStr = new System.IO.FileStream(outName, System.IO.FileMode.Create); tarIn.CopyEntryContents(outStr); outStr.Close(); DateTime myDt = DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc); System.IO.File.SetLastWriteTimeUtc(outName, myDt); } tarIn.Close(); } }
public StatusCode Exists(Models.FileTransportInfo transportInformation, out bool exist) { StatusCode retVal = init(); exist = false; SftpClient client = null; try { using (client = new SftpClient(connectionInformation)) { client.Connect(); exist = client.Exists(transportInformation.SourceFullName); client.Disconnect(); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_LISTUP_ERROR, new object[] { connectionInformation.Host, transportInformation.SourceFolderName }, ex, Config, ApplicationID)); } finally { } return(StatusCode.SUCCEED_STATUS); }
public StatusCode Upload(Models.FileTransportInfo transportInformation) { StatusCode retVal = StatusCode.SUCCEED_STATUS; if (client == null || client.IsConnected == false) { retVal = Open(); if (retVal.IsSucceeded == false) { return(retVal); } } try { if (transportInformation.SourceIsDirectory) { client.Upload(new System.IO.DirectoryInfo(transportInformation.SourceFullName), transportInformation.DestinationFolderName); } else { client.Upload(new System.IO.FileInfo(transportInformation.SourceFullName), transportInformation.DestinationFolderName); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SCP_UPLOAD_ERROR, new object[] { connectionInformation.Host, connectionInformation.Username, transportInformation.SourceFullName, transportInformation.DestinationFolderName }, ex, Config, ApplicationID)); } return(retVal); }
public StatusCode GetFileInfo(Models.FileTransportInfo transportInformation, out long lSize, out DateTime updateTime) { lSize = -1; updateTime = DateTime.Today; StatusCode retVal = init(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } try { FtpListItem item = request.GetObjectInfo(transportInformation.SourceFullName); lSize = item.Size; updateTime = item.Modified; } catch (Exception ex) { object[] args = new object[2]; args[0] = transportInformation.SourceFullName; args[1] = "**"; return(new Common.CommonStatusCode(Common.CommonStatusCode.FTP_GETSIZE_ERROR, args, ex, Config, ApplicationID)); } return(retVal); }
public StatusCode ListFiles(Models.FileTransportInfo transportInformation, out List <string> lstFile) { StatusCode retVal = init(); lstFile = new List <string>(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } try { foreach (FtpListItem item in request.GetListing(transportInformation.SourceFolderName)) { if (item.Type == FtpFileSystemObjectType.File) { if (string.IsNullOrEmpty(transportInformation.SourceFileName)) { lstFile.Add(item.Name); } else { if (Regex.IsMatch(item.Name, transportInformation.SourceFileName)) { lstFile.Add(item.Name); } } } } } catch (Exception) {} return(retVal); }
public StatusCode GetModifiedDateSize(Models.FileTransportInfo transportInformation, out DateTime updateTime, out long lSize) { lSize = -1; updateTime = DateTime.Today; StatusCode retVal = init(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } SftpClient client = null; try { using (client = new SftpClient(connectionInformation)) { client.Connect(); Renci.SshNet.Sftp.SftpFileAttributes attr = client.GetAttributes(transportInformation.SourceFullName); lSize = attr.Size; updateTime = attr.LastWriteTime; client.Disconnect(); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_WRITETIME_ERROR, new object[] { connectionInformation.Host, transportInformation.SourceFolderName, transportInformation.SourceFileName }, ex, Config, ApplicationID)); } finally { } return(retVal); }
public StatusCode Download(Models.FileTransportInfo transportInformation, out System.IO.Stream st) { st = null; StatusCode retVal = init(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } try { streamingClient = new SftpClient(connectionInformation); streamingClient.Connect(); st = streamingClient.OpenRead(transportInformation.SourceFullName); } catch (Renci.SshNet.Common.SftpPathNotFoundException spnex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_FILE_NOT_FOUND, new object[] { connectionInformation.Host, transportInformation.SourceFolderName, transportInformation.SourceFileName }, spnex, Config, ApplicationID)); } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_DOWNLOAD_ERROR, new object[] { connectionInformation.Host, transportInformation.SourceFolderName, transportInformation.SourceFileName }, ex, Config, ApplicationID)); } finally { } return(retVal); }
internal static void Extract(Models.FileTransportInfo fileTransferInfo) { using (System.IO.FileStream fsIn = new System.IO.FileStream(fileTransferInfo.SourceFullNameWithBasePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { Extract(fsIn, fileTransferInfo); } }
internal List <string> GetEntryFileNamesFromDirectory(Models.FileTransportInfo info) { List <string> lstEntry = new List <string>(); if (info.SourceIsDirectory == false) { return(lstEntry); } if (zipObject == null) { return(lstEntry); } string searchEntryPrefix = ZipEntry.CleanName(info.SourceFolderName) + "/"; foreach (ZipEntry entry in zipObject) { if (entry.IsDirectory) { continue; } if (entry.Name.StartsWith(searchEntryPrefix)) { lstEntry.Add(entry.Name); } } return(lstEntry); }
public void BZipSimpleArchive() { //Create sample file (text) string RootPath = System.IO.Path.GetTempPath(); string TestFile1Name = "bzipTest.txt"; string TestFile1FullPath = RootPath + TestFile1Name; if (System.IO.File.Exists(TestFile1FullPath)) { System.IO.File.Delete(TestFile1FullPath); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile1FullPath)) { writer.WriteLine("TEST Data aabcdfkdfaa"); writer.WriteLine("1234566"); writer.Flush(); } string OriginalContent = null; using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile1FullPath)) { OriginalContent = reader.ReadToEnd(); } string BZipFilename = "BZipTest.bz2"; string BZipFullpath = RootPath + BZipFilename; if (System.IO.File.Exists(BZipFullpath)) { System.IO.File.Delete(BZipFullpath); } //Create zip file from this sample file Models.FileTransportInfo transport = new Models.FileTransportInfo(null, RootPath, TestFile1Name, RootPath, BZipFilename); Gyomu.Common.Archive.BZip2Archive.Create(transport); System.IO.File.Delete(TestFile1FullPath); //Extract from the zip file and compare with original text file transport = new Models.FileTransportInfo(null, RootPath, BZipFilename, RootPath, TestFile1Name); Gyomu.Common.Archive.BZip2Archive.Extract(transport); using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile1FullPath)) { Assert.Equal(OriginalContent, reader.ReadToEnd()); } System.IO.File.Delete(TestFile1FullPath); using (System.IO.Stream st = Gyomu.Common.Archive.BZip2Archive.Extract(BZipFullpath)) { using (System.IO.StreamReader reader = new System.IO.StreamReader(st)) { Assert.Equal(OriginalContent, reader.ReadToEnd()); } } System.IO.File.Delete(BZipFullpath); }
private static void archiveSimpleTest() { //Create sample file (text) string RootPath = System.IO.Path.GetTempPath(); string TestFilePath = RootPath + System.IO.Path.DirectorySeparatorChar + "zipFolder"; if (System.IO.Directory.Exists(TestFilePath) == false) { System.IO.Directory.CreateDirectory(TestFilePath); } string TestFile1Name = "zipTest.txt"; string TestFile1FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile1Name; if (System.IO.File.Exists(TestFile1FullPath)) { System.IO.File.Delete(TestFile1FullPath); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile1FullPath)) { writer.WriteLine("TEST Data aabcdfkdfaa"); writer.WriteLine("1234566"); writer.Flush(); } string OriginalContent = null; using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile1FullPath)) { OriginalContent = reader.ReadToEnd(); } string ZipFilename = System.IO.Path.GetTempPath() + "ZipTest.zip"; if (System.IO.File.Exists(ZipFilename)) { System.IO.File.Delete(ZipFilename); } //Create zip file from this sample file Models.FileTransportInfo transport = new Models.FileTransportInfo(RootPath, "zipFolder", TestFile1Name, null, null); Gyomu.Common.Archive.ZipArchive.Create(ZipFilename, new List <Models.FileTransportInfo>() { transport }); System.IO.File.Delete(TestFile1FullPath); //Extract from the zip file and compare with original text file using (Gyomu.Common.Archive.ZipArchive archive = new Gyomu.Common.Archive.ZipArchive(ZipFilename)) { System.IO.Stream stream = archive.GetEntryFileFromFile(transport); using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) Assert.Equal(OriginalContent, reader.ReadToEnd()); } System.IO.File.Delete(ZipFilename); }
internal System.IO.Stream GetEntryFileFromFile(Models.FileTransportInfo info) { if (info == null) { return(null); } if (info.SourceIsDirectory) { return(null); } return(GetEntryFileFromName(getEntryFileFromSourceZip(info))); }
public StatusCode ListFiles(Models.FileTransportInfo transportInformation, out List <string> lstFile) { StatusCode retVal = init(); lstFile = new List <string>(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } SftpClient client = null; try { using (client = new SftpClient(connectionInformation)) { client.Connect(); foreach (Renci.SshNet.Sftp.SftpFile file in client.ListDirectory(transportInformation.SourceFolderName)) { if (transportInformation.SourceFileName == null || transportInformation.SourceFileName.Length == 0) { lstFile.Add(file.Name); } else { if (Regex.IsMatch(file.Name, transportInformation.SourceFileName)) { lstFile.Add(file.Name); break; } } } client.Disconnect(); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_LISTUP_ERROR, new object[] { connectionInformation.Host, transportInformation.SourceFolderName }, ex, Config, ApplicationID)); } finally { } return(StatusCode.SUCCEED_STATUS); }
internal void Archive(Models.FileTransportInfo fileTransportInfo) { if (fileTransportInfo.SourceIsDirectory == false) { string entryName = ZipEntry.CleanName(fileTransportInfo.DestinationFolderName + @"\" + fileTransportInfo.DestinationFileName); System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileTransportInfo.SourceFullNameWithBasePath); zipObject.Add(fileInfo.FullName, entryName); } else { string[] files = System.IO.Directory.GetFiles(fileTransportInfo.SourceFullNameWithBasePath, "*", System.IO.SearchOption.AllDirectories); ZipNameTransform nameTransform = new ZipNameTransform(fileTransportInfo.BasePath); foreach (string filename in files) { string entryName = nameTransform.TransformFile(filename); zipObject.Add(filename, entryName); } } }
internal static void Unzip(Models.FileTransportInfo fileTransferInfo, string password = null) { ZipArchive archive = new ZipArchive() { zipObject = new ZipFile(fileTransferInfo.SourceFullNameWithBasePath) }; using (archive) { archive.zipObject.UseZip64 = UseZip64.Dynamic; if (!string.IsNullOrEmpty(password)) { archive.zipObject.Password = password; } //if (fileTransferInfo.SourceFullName == null) { foreach (ZipEntry zipEntry in archive.zipObject) { if (!zipEntry.IsFile) { continue; } string entryFilename = zipEntry.Name; var fullZipToPath = System.IO.Path.Combine(fileTransferInfo.DestinationFolderName, entryFilename); var directoryName = System.IO.Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { System.IO.Directory.CreateDirectory(directoryName); } var buffer = new byte[4096]; using (Stream fsOutput = File.Create(fullZipToPath)) { StreamUtils.Copy(archive.GetEntryFileFromName(entryFilename), fsOutput, buffer); } } //} } }
public StatusCode Download(Models.FileTransportInfo transportInformation) { StatusCode retVal = StatusCode.SUCCEED_STATUS; if (client == null || client.IsConnected == false) { retVal = Open(); if (retVal.IsSucceeded == false) { return(retVal); } } try { if (transportInformation.SourceIsDirectory) { DirectoryInfo directoryInformation = new DirectoryInfo(transportInformation.DestinationFullName); if (directoryInformation.Exists) { directoryInformation.Delete(); } client.Download(transportInformation.SourceFolderName, directoryInformation); } else { FileInfo fileInformation = new FileInfo(transportInformation.DestinationFullName); if (fileInformation.Exists) { fileInformation.Delete(); } client.Download(transportInformation.SourceFullName, fileInformation); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SCP_DOWNLOAD_ERROR, new object[] { connectionInformation.Host, connectionInformation.Username, transportInformation.SourceFullName, transportInformation.DestinationFullName }, ex, Config, ApplicationID)); } return(retVal); }
internal static void Create(Models.FileTransportInfo fileTransferInfo) { if (fileTransferInfo.SourceIsDirectory == false) { throw new InvalidOperationException("File archive is not supported. Need to support folder only"); } using (System.IO.Stream outStream = System.IO.File.Create(fileTransferInfo.DestinationFullName)) { using (ICSharpCode.SharpZipLib.Tar.TarArchive tarArchive = ICSharpCode.SharpZipLib.Tar.TarArchive.CreateOutputTarArchive(outStream)) { tarArchive.RootPath = fileTransferInfo.BasePath.Replace(@"\", "/"); if (tarArchive.RootPath.EndsWith("/")) { tarArchive.RootPath = tarArchive.RootPath.Remove(tarArchive.RootPath.Length - 1); } AddDirectoryFilesToTar(tarArchive, fileTransferInfo.SourceFullNameWithBasePath, true); tarArchive.Close(); } } }
public StatusCode Download(Models.FileTransportInfo transportInformation) { StatusCode retVal = init(); if (retVal != StatusCode.SUCCEED_STATUS) { return(retVal); } SftpClient client = null; try { using (client = new SftpClient(connectionInformation)) { client.Connect(); using (System.IO.FileStream fs = new System.IO.FileStream(transportInformation.DestinationFullName, System.IO.FileMode.Create)) { client.DownloadFile(transportInformation.SourceFullName, fs); } client.Disconnect(); } } catch (Renci.SshNet.Common.SftpPathNotFoundException spnex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_FILE_NOT_FOUND, new object[] { connectionInformation.Host, transportInformation.SourceFolderName, transportInformation.SourceFileName }, spnex, Config, ApplicationID)); } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.SFTP_DOWNLOAD_ERROR, new object[] { connectionInformation.Host, transportInformation.SourceFolderName, transportInformation.SourceFileName }, ex, Config, ApplicationID)); } finally { } return(retVal); }
public StatusCode Upload(Models.FileTransportInfo transportInformation, bool isBinary) { StatusCode retVal = StatusCode.SUCCEED_STATUS; try { retVal = init(); if (retVal.IsSucceeded == false) { return(retVal); } request.UploadDataType = isBinary ? FtpDataType.Binary : FtpDataType.ASCII; bool result = request.UploadFile(transportInformation.SourceFullName, transportInformation.DestinationFullName, FtpExists.Overwrite); if (result == false) { return(new Common.CommonStatusCode(Common.CommonStatusCode.FTP_DOWNLOAD_ERROR, new object[] { transportInformation.SourceFullName, transportInformation.DestinationFullName, "Not sure" }, Config, ApplicationID)); } } catch (Exception ex) { return(new Common.CommonStatusCode(Common.CommonStatusCode.FTP_DOWNLOAD_ERROR, new object[] { transportInformation.SourceFullName, transportInformation.DestinationFullName, "Not sure" }, ex, Config, ApplicationID)); } return(StatusCode.SUCCEED_STATUS); }
internal static void Extract(Models.FileTransportInfo fileTransferInfo) { BZip2.Decompress(System.IO.File.OpenRead(fileTransferInfo.SourceFullName), System.IO.File.OpenWrite(fileTransferInfo.DestinationFullName), true); }
internal static void Create(Models.FileTransportInfo fileTransferInfo) { BZip2.Compress(System.IO.File.OpenRead(fileTransferInfo.SourceFullName), System.IO.File.Create(fileTransferInfo.DestinationFullName), true, 9); }
public static StatusCode UnarchiveAllAndCreateDestinationFolderIfNotExist(string archiveFilename, ArchiveType archiveType, Models.FileTransportInfo transport, Common.Configurator Config, short ApplicationID, string password = null) { if (transport == null) { return(StatusCode.InvalidArgumentStatus("Transportation Information Not Specified to archive", Config, ApplicationID)); } FileInfo archiveInfo = new System.IO.FileInfo(archiveFilename); if (archiveType == ArchiveType.GuessFromFileName) { switch (archiveInfo.Extension.ToUpper()) { case ".ZIP": archiveType = ArchiveType.Zip; break; case ".TGZ": archiveType = ArchiveType.TGZ; break; case ".BZ2": archiveType = ArchiveType.BZip2; break; case ".GZ": archiveType = ArchiveType.GZip; break; case ".TAR": archiveType = ArchiveType.Tar; break; default: return(StatusCode.InvalidArgumentStatus("File Extension Not supported for archiving", Config, ApplicationID)); } } if (archiveType == ArchiveType.BZip2 || archiveType == ArchiveType.GZip) { if (transport.SourceIsDirectory) { return(StatusCode.InvalidArgumentStatus("Multiple files are not supported in this compression type", Config, ApplicationID)); } } if (archiveType != ArchiveType.Zip && string.IsNullOrEmpty(password) == false) { return(StatusCode.InvalidArgumentStatus("password is not supported on other than zip format", Config, ApplicationID)); } string archiveFileDirectory = archiveInfo.Directory.FullName; string archiveName = archiveInfo.Name; System.IO.Directory.CreateDirectory(transport.DestinationFolderName); switch (archiveType) { case ArchiveType.Zip: transport.SourceFolderName = archiveFileDirectory; transport.SourceFileName = archiveName; Common.Archive.ZipArchive.Unzip(transport); break; case ArchiveType.BZip2: transport.SourceFolderName = archiveFileDirectory; transport.SourceFileName = archiveName; Common.Archive.BZip2Archive.Extract(transport); break; case ArchiveType.GZip: transport.SourceFolderName = archiveFileDirectory; transport.SourceFileName = archiveName; Common.Archive.GZipArchive.Extract(transport); break; case ArchiveType.Tar: transport.SourceFolderName = archiveFileDirectory; transport.SourceFileName = archiveName; Common.Archive.TarArchive.Extract(transport); break; case ArchiveType.TGZ: transport.SourceFolderName = archiveFileDirectory; transport.SourceFileName = archiveName; string originalDestinationFolder = transport.DestinationFolderName; string originalDestinationFile = transport.DestinationFileName; transport.DestinationFolderName = Path.GetTempPath(); transport.DestinationFileName = archiveName.Substring(0, archiveName.Length - (archiveInfo.Extension.Length)) + ".tar"; Common.Archive.GZipArchive.Extract(transport); Models.FileTransportInfo tarInfo = new Models.FileTransportInfo(null, transport.DestinationFolderName, transport.DestinationFileName, originalDestinationFolder, originalDestinationFile); Common.Archive.TarArchive.Extract(tarInfo); File.Delete(transport.DestinationFullName); break; } return(StatusCode.SUCCEED_STATUS); }
public static StatusCode Archive(string archiveFilename, ArchiveType archiveType, List <Models.FileTransportInfo> SourceFiles, Common.Configurator Config, short ApplicationID, string password = null) { if (SourceFiles == null || SourceFiles.Count == 0) { return(StatusCode.InvalidArgumentStatus("Source File Not Specified to archive", Config, ApplicationID)); } FileInfo archiveInfo = new System.IO.FileInfo(archiveFilename); if (archiveType == ArchiveType.GuessFromFileName) { switch (archiveInfo.Extension.ToUpper()) { case ".ZIP": archiveType = ArchiveType.Zip; break; case ".TGZ": archiveType = ArchiveType.TGZ; break; case ".BZ2": archiveType = ArchiveType.BZip2; break; case ".GZ": archiveType = ArchiveType.GZip; break; case ".TAR": archiveType = ArchiveType.Tar; break; default: return(StatusCode.InvalidArgumentStatus("File Extension Not supported for archiving", Config, ApplicationID)); } } if (archiveType == ArchiveType.BZip2 || archiveType == ArchiveType.GZip) { if (SourceFiles.Count > 1 || SourceFiles.Where(f => f.SourceIsDirectory).Count() > 0) { return(StatusCode.InvalidArgumentStatus("Multiple files are not supported in this compression type", Config, ApplicationID)); } } if (archiveType != ArchiveType.Zip && string.IsNullOrEmpty(password) == false) { return(StatusCode.InvalidArgumentStatus("password is not supported on other than zip format", Config, ApplicationID)); } Models.FileTransportInfo transport = SourceFiles[0]; string archiveFileDirectory = archiveInfo.Directory.FullName; string archiveName = archiveInfo.Name; switch (archiveType) { case ArchiveType.Zip: Common.Archive.ZipArchive.Create(archiveFilename, SourceFiles, password); break; case ArchiveType.BZip2: transport.DestinationFolderName = archiveFileDirectory; transport.DestinationFileName = archiveName; Common.Archive.BZip2Archive.Create(transport); break; case ArchiveType.GZip: transport.DestinationFolderName = archiveFileDirectory; transport.DestinationFileName = archiveName; Common.Archive.GZipArchive.Create(transport); break; case ArchiveType.Tar: transport.DestinationFolderName = archiveFileDirectory; transport.DestinationFileName = archiveName; Common.Archive.TarArchive.Create(transport); break; case ArchiveType.TGZ: transport.DestinationFolderName = Path.GetTempPath(); transport.DestinationFileName = archiveName.Substring(0, archiveName.Length - (archiveInfo.Extension.Length)) + ".tar"; Common.Archive.TarArchive.Create(transport); Models.FileTransportInfo tgzInfo = new Models.FileTransportInfo(null, transport.DestinationFolderName, transport.DestinationFileName, archiveFileDirectory, archiveName); Common.Archive.GZipArchive.Create(tgzInfo); break; } return(StatusCode.SUCCEED_STATUS); }
public void FileArchiveTest() { string RootPath = System.IO.Path.GetTempPath(); string TestFileDirectory = "archiveFolder"; string TestFilePath = RootPath + System.IO.Path.DirectorySeparatorChar + TestFileDirectory; if (System.IO.Directory.Exists(TestFilePath) == false) { System.IO.Directory.CreateDirectory(TestFilePath); } string TestFile1Name = "archiveTest1.txt"; string TestFile1FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile1Name; if (System.IO.File.Exists(TestFile1FullPath)) { System.IO.File.Delete(TestFile1FullPath); } Gyomu.Common.Configurator config = Gyomu.Common.BaseConfigurator.GetInstance(); config.ApplicationID = Common.GyomuDataAccessTest.testApplicationId; short testApplicationID = Common.GyomuDataAccessTest.testApplicationId; #region Init part string TestFile2Name = "archiveTest2.txt"; string TestFile2FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile2Name; if (System.IO.File.Exists(TestFile2FullPath)) { System.IO.File.Delete(TestFile2FullPath); } string TestFileSubPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + "zipSubFolder"; if (System.IO.Directory.Exists(TestFileSubPath) == false) { System.IO.Directory.CreateDirectory(TestFileSubPath); } string TestFile3Name = "archiveTest3.txt"; string TestFile3FullPath = TestFileSubPath + System.IO.Path.DirectorySeparatorChar + TestFile3Name; if (System.IO.File.Exists(TestFile3FullPath)) { System.IO.File.Delete(TestFile3FullPath); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile1FullPath)) { writer.WriteLine("TEST Data aabcdfkdfaa"); writer.WriteLine("1234566"); writer.Flush(); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile2FullPath)) { writer.WriteLine("TEST Data a22343afda"); writer.WriteLine("1234566"); writer.Flush(); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile3FullPath)) { writer.WriteLine("TEST Data 322343afda"); writer.WriteLine("1234566"); writer.Flush(); } #endregion Gyomu.Models.FileTransportInfo transport = new Models.FileTransportInfo(TestFilePath, null, null, null, null); string ZipFilename = RootPath + "zipArchive.zip"; if (System.IO.File.Exists(ZipFilename)) { System.IO.File.Delete(ZipFilename); } StatusCode retVal = Gyomu.Access.FileOperationAccess.Archive(ZipFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, new List <Models.FileTransportInfo>() { transport }, config, testApplicationID); Assert.True(retVal.IsSucceeded); string ZipExtractPath = RootPath + System.IO.Path.DirectorySeparatorChar + "zipArchiveFolder"; if (System.IO.Directory.Exists(ZipExtractPath)) { System.IO.Directory.Delete(ZipExtractPath, true); } Models.FileTransportInfo zipTransport = new Models.FileTransportInfo(null, null, null, ZipExtractPath, null); retVal = Gyomu.Access.FileOperationAccess.UnarchiveAllAndCreateDestinationFolderIfNotExist(ZipFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, zipTransport, config, testApplicationID); Assert.True(retVal.IsSucceeded); compareFile(TestFilePath, ZipExtractPath); #region Tar transport = new Models.FileTransportInfo(TestFilePath, null, null, null, null); string TarFilename = RootPath + "tarArchive.tar"; if (System.IO.File.Exists(TarFilename)) { System.IO.File.Delete(TarFilename); } retVal = Gyomu.Access.FileOperationAccess.Archive(TarFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, new List <Models.FileTransportInfo>() { transport }, config, testApplicationID); Assert.True(retVal.IsSucceeded); string TarExtractPath = RootPath + System.IO.Path.DirectorySeparatorChar + "tarArchiveFolder"; if (System.IO.Directory.Exists(TarExtractPath)) { System.IO.Directory.Delete(TarExtractPath, true); } Models.FileTransportInfo tarTransport = new Models.FileTransportInfo(null, null, null, TarExtractPath, null); retVal = Gyomu.Access.FileOperationAccess.UnarchiveAllAndCreateDestinationFolderIfNotExist(TarFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, tarTransport, config, testApplicationID); Assert.True(retVal.IsSucceeded); compareFile(TestFilePath, TarExtractPath); #endregion #region TGZ transport = new Models.FileTransportInfo(TestFilePath, null, null, null, null); string TgzFilename = RootPath + "tgzArchive.tgz"; if (System.IO.File.Exists(TgzFilename)) { System.IO.File.Delete(TgzFilename); } retVal = Gyomu.Access.FileOperationAccess.Archive(TgzFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, new List <Models.FileTransportInfo>() { transport }, config, testApplicationID); Assert.True(retVal.IsSucceeded); string TgzExtractPath = RootPath + System.IO.Path.DirectorySeparatorChar + "tgzArchiveFolder"; if (System.IO.Directory.Exists(TgzExtractPath)) { System.IO.Directory.Delete(TgzExtractPath, true); } Models.FileTransportInfo tgzTransport = new Models.FileTransportInfo(null, null, null, TgzExtractPath, null); retVal = Gyomu.Access.FileOperationAccess.UnarchiveAllAndCreateDestinationFolderIfNotExist(TgzFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, tgzTransport, config, testApplicationID); Assert.True(retVal.IsSucceeded); compareFile(TestFilePath, TgzExtractPath); #endregion #region GZip transport = new Models.FileTransportInfo(TestFilePath, null, TestFile1Name, null, null); string GzipFilename = RootPath + "gzArchive.gz"; if (System.IO.File.Exists(GzipFilename)) { System.IO.File.Delete(GzipFilename); } retVal = Gyomu.Access.FileOperationAccess.Archive(GzipFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, new List <Models.FileTransportInfo>() { transport }, config, testApplicationID); Assert.True(retVal.IsSucceeded); string GzipExtractPath = RootPath + System.IO.Path.DirectorySeparatorChar + "gzArchiveFolder"; if (System.IO.Directory.Exists(GzipExtractPath)) { System.IO.Directory.Delete(GzipExtractPath, true); } Models.FileTransportInfo gzipTransport = new Models.FileTransportInfo(null, null, GzipFilename, GzipExtractPath, TestFile1Name); retVal = Gyomu.Access.FileOperationAccess.UnarchiveAllAndCreateDestinationFolderIfNotExist(GzipFilename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, gzipTransport, config, testApplicationID); Assert.True(retVal.IsSucceeded); compareFile(TestFile1FullPath, GzipExtractPath + System.IO.Path.DirectorySeparatorChar + TestFile1Name); #endregion #region BZip2 transport = new Models.FileTransportInfo(TestFilePath, null, TestFile1Name, null, null); string BZip2Filename = RootPath + "bzip2Archive.gz"; if (System.IO.File.Exists(BZip2Filename)) { System.IO.File.Delete(BZip2Filename); } retVal = Gyomu.Access.FileOperationAccess.Archive(BZip2Filename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, new List <Models.FileTransportInfo>() { transport }, config, testApplicationID); Assert.True(retVal.IsSucceeded); string Bzip2ExtractPath = RootPath + System.IO.Path.DirectorySeparatorChar + "bzip2ArchiveFolder"; if (System.IO.Directory.Exists(Bzip2ExtractPath)) { System.IO.Directory.Delete(Bzip2ExtractPath, true); } Models.FileTransportInfo bzip2Transport = new Models.FileTransportInfo(null, null, BZip2Filename, Bzip2ExtractPath, TestFile1Name); retVal = Gyomu.Access.FileOperationAccess.UnarchiveAllAndCreateDestinationFolderIfNotExist(BZip2Filename, Gyomu.Access.FileOperationAccess.ArchiveType.GuessFromFileName, bzip2Transport, config, testApplicationID); Assert.True(retVal.IsSucceeded); compareFile(TestFile1FullPath, Bzip2ExtractPath + System.IO.Path.DirectorySeparatorChar + TestFile1Name); #endregion }
private static void archiveSimpleTest() { //Create sample file (text) string RootPath = System.IO.Path.GetTempPath(); string TestFilePath = RootPath + System.IO.Path.DirectorySeparatorChar + "tarFolder"; if (System.IO.Directory.Exists(TestFilePath)) { System.IO.Directory.Delete(TestFilePath, true); } System.IO.Directory.CreateDirectory(TestFilePath); string TestFile1Name = "tarTest.txt"; string TestFile1FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile1Name; if (System.IO.File.Exists(TestFile1FullPath)) { System.IO.File.Delete(TestFile1FullPath); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile1FullPath)) { writer.WriteLine("TEST Data aabcdfkdfaa"); writer.WriteLine("1234566"); writer.Flush(); } string OriginalContent = null; using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile1FullPath)) { OriginalContent = reader.ReadToEnd(); } string TarFilename = "TarTest.tar"; string TarFullpath = System.IO.Path.GetTempPath() + TarFilename; if (System.IO.File.Exists(TarFullpath)) { System.IO.File.Delete(TarFullpath); } //Create tar file from this sample file Models.FileTransportInfo transport = new Models.FileTransportInfo(RootPath, "tarFolder", null, RootPath, TarFilename); Gyomu.Common.Archive.TarArchive.Create(transport); System.IO.File.Delete(TestFile1FullPath); //Extract from the tar file and compare with original text file string TestExtractRootPath = RootPath + System.IO.Path.DirectorySeparatorChar + "tarExtractFolder"; if (System.IO.Directory.Exists(TestExtractRootPath) == false) { System.IO.Directory.CreateDirectory(TestExtractRootPath); } transport = new Models.FileTransportInfo(null, RootPath, TarFilename, TestExtractRootPath, null); Gyomu.Common.Archive.TarArchive.Extract(transport); List <System.IO.FileInfo> fileInfoList = Gyomu.Access.FileOperationAccess.Search(TestExtractRootPath, new List <Models.FileFilterInfo>() { new Models.FileFilterInfo(Models.FileFilterInfo.FilterType.FileName, Models.FileFilterInfo.CompareType.Equal, TestFile1Name) }, true); Assert.NotNull(fileInfoList); Assert.NotEmpty(fileInfoList); Assert.Single(fileInfoList); System.IO.FileInfo fileInfo = fileInfoList[0]; using (System.IO.StreamReader reader = new System.IO.StreamReader(fileInfo.FullName)) { Assert.Equal(OriginalContent, reader.ReadToEnd()); } }
private static void archiveDirectoryTest() { //Create sample file (text) string RootPath = System.IO.Path.GetTempPath(); string TestFilePath = RootPath + System.IO.Path.DirectorySeparatorChar + "zipFolder2"; if (System.IO.Directory.Exists(TestFilePath) == false) { System.IO.Directory.CreateDirectory(TestFilePath); } string TestFile1Name = "zipTest1.txt"; string TestFile1FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile1Name; if (System.IO.File.Exists(TestFile1FullPath)) { System.IO.File.Delete(TestFile1FullPath); } string TestFile2Name = "zipTest2.txt"; string TestFile2FullPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + TestFile2Name; if (System.IO.File.Exists(TestFile2FullPath)) { System.IO.File.Delete(TestFile2FullPath); } string TestFileSubPath = TestFilePath + System.IO.Path.DirectorySeparatorChar + "zipSubFolder"; if (System.IO.Directory.Exists(TestFileSubPath) == false) { System.IO.Directory.CreateDirectory(TestFileSubPath); } string TestFile3Name = "zipTest3.txt"; string TestFile3FullPath = TestFileSubPath + System.IO.Path.DirectorySeparatorChar + TestFile3Name; if (System.IO.File.Exists(TestFile3FullPath)) { System.IO.File.Delete(TestFile3FullPath); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile1FullPath)) { writer.WriteLine("TEST Data aabcdfkdfaa"); writer.WriteLine("1234566"); writer.Flush(); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile2FullPath)) { writer.WriteLine("TEST Data a22343afda"); writer.WriteLine("1234566"); writer.Flush(); } using (System.IO.StreamWriter writer = new System.IO.StreamWriter(TestFile3FullPath)) { writer.WriteLine("TEST Data 322343afda"); writer.WriteLine("1234566"); writer.Flush(); } List <string> OriginalContents = new List <string>(); using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile1FullPath)) { OriginalContents.Add(reader.ReadToEnd()); } using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile2FullPath)) { OriginalContents.Add(reader.ReadToEnd()); } using (System.IO.StreamReader reader = new System.IO.StreamReader(TestFile3FullPath)) { OriginalContents.Add(reader.ReadToEnd()); } string ZipFilename = System.IO.Path.GetTempPath() + "ZipFolderTest.zip"; if (System.IO.File.Exists(ZipFilename)) { System.IO.File.Delete(ZipFilename); } //Create zip file from this sample file Models.FileTransportInfo transport = new Models.FileTransportInfo(RootPath, "zipFolder2", null, null, null); Gyomu.Common.Archive.ZipArchive.Create(ZipFilename, new List <Models.FileTransportInfo>() { transport }); using (Gyomu.Common.Archive.ZipArchive archive = new Gyomu.Common.Archive.ZipArchive(ZipFilename)) { List <string> entryNames = archive.GetEntryFileNamesFromDirectory(transport); foreach (string entryName in entryNames) { System.IO.Stream stream = archive.GetEntryFileFromName(entryName); using (System.IO.StreamReader reader = new System.IO.StreamReader(stream)) Assert.Contains(reader.ReadToEnd(), OriginalContents); } } //System.IO.File.Delete(TestFile1FullPath); //System.IO.File.Delete(ZipFilename); }
private static string getEntryFileFromSourceZip(Models.FileTransportInfo info) { return(ZipEntry.CleanName(info.SourceFullName)); }