ASCU_ALL/DataClients/FileClient.cs

139 lines
3.5 KiB
C#
Raw Normal View History

2020-09-04 12:49:15 +05:00
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
2020-11-18 16:56:28 +05:00
using SupportClasses;
2020-09-04 12:49:15 +05:00
namespace DataClients
{
public class FileClient
{
2020-11-18 16:56:28 +05:00
private char split = _Directory.Slash;
//private string dir = @"Y:\data";
2020-09-04 12:49:15 +05:00
//private string dir = @"C:\data";
2020-11-18 16:56:28 +05:00
private string dir;
public string Dir {
get { return dir; }
set { dir = value; }
}
2020-09-04 12:49:15 +05:00
public FileClient()
{
2020-11-18 16:56:28 +05:00
fileClient(@"/archive_rmt/data");
//fileClient(@"Y:\data");
//fileClient(@"C:\data");
2020-09-04 12:49:15 +05:00
}
public FileClient(string directory)
2020-11-18 16:56:28 +05:00
{
fileClient(directory);
}
private void fileClient(string directory)
2020-09-04 12:49:15 +05:00
{
TempDir.StartCkeckDir();
2020-11-18 16:56:28 +05:00
Dir = directory;
2020-09-04 12:49:15 +05:00
}
public byte[] GetFile(DateTime time, ushort vdp, ushort index)
{
var name = time.ToString("yyyyMMdd") + "." + vdp.ToString("D2") + index.ToString("D1");
var tmpFileName = TempDir.GetTempDirectory() + split + name;
if (!TempDir.IsExist(name))
{
if (!CheckArchive(time, vdp)) return new byte[0];
if (!ExtractArchive(time, vdp)) return new byte[0];
}
if (!TempDir.IsExist(name)) return new byte[0];
while (TempDir.IsProtect(name)) Thread.Sleep(1000);
TempDir.AddProtect(name);
2020-11-18 16:56:28 +05:00
using (var fs = (Stream)File.OpenRead(tmpFileName))
2020-09-04 12:49:15 +05:00
{
2020-11-18 16:56:28 +05:00
var result = new List<byte>();
try
{
for (var i = 0; i < fs.Length; i++)
result.Add((byte)fs.ReadByte());
return result.ToArray();
}
catch (Exception e)
{
Logger.Add("Can't read file: " + name, Logger.Level.error);
Logger.Add(e.Message, Logger.Level.error);
Logger.Add(e.StackTrace, Logger.Level.error);
return new byte[0];
}
finally
{
TempDir.RemoveProtect(name);
}
2020-09-04 12:49:15 +05:00
}
}
private bool CheckArchive(DateTime time, ushort vdp)
{
2020-11-18 16:56:28 +05:00
var a =
2020-09-04 12:49:15 +05:00
dir + split +
time.Year.ToString("D4") + split +
time.Month.ToString("D2") + split +
time.Day.ToString("D2") + split +
vdp.ToString("D2");
2020-11-18 16:56:28 +05:00
return (File.Exists(a + ".tar") || File.Exists(a + ".tar.gz"));
2020-09-04 12:49:15 +05:00
}
private bool ExtractArchive(DateTime time, ushort vdp)
{
var tmp =
dir + split +
time.Year.ToString("D4") + split +
time.Month.ToString("D2") + split +
time.Day.ToString("D2") + split +
vdp.ToString("D2");
if (File.Exists(tmp + ".tar"))
{
ExtractTar(tmp + ".tar", TempDir.GetTempDirectory());
return true;
}
else if (File.Exists(tmp + ".tar.gz"))
{
ExtractTarGZ(tmp + ".tar.gz", TempDir.GetTempDirectory());
return true;
}
return false;
}
private void ExtractTarGZ(String gzArchiveName, String destFolder)
{
2020-11-18 16:56:28 +05:00
using (var a = (Stream)File.OpenRead(gzArchiveName))
using (var b = (Stream)new GZipInputStream(a))
try
{
using (var c = TarArchive.CreateInputTarArchive(b))
c.ExtractContents(destFolder);
}
catch (Exception e)
{
Logger.Add("Can't extract: " + gzArchiveName, Logger.Level.error);
Logger.Add(e.Message, Logger.Level.error);
Logger.Add(e.StackTrace, Logger.Level.error);
}
2020-09-04 12:49:15 +05:00
}
private void ExtractTar(String tarFileName, String destFolder)
{
2020-11-18 16:56:28 +05:00
using (var a = (Stream)File.OpenRead(tarFileName))
try
{
using (var b = TarArchive.CreateInputTarArchive(a))
b.ExtractContents(destFolder);
}
catch (Exception e)
{
Logger.Add("Can't extract: " + tarFileName, Logger.Level.error);
Logger.Add(e.Message, Logger.Level.error);
Logger.Add(e.StackTrace, Logger.Level.error);
}
2020-09-04 12:49:15 +05:00
}
}
}