ASCU_ALL/DataClients/FileClient.cs

137 lines
3.3 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;
namespace DataClients
{
public class FileClient
{
2020-09-29 08:18:25 +05:00
//private char split = '/';
private char split = '\\';
private string dir = @"Y:\data";
2020-09-04 12:49:15 +05:00
//private string dir = @"C:\data";
2020-09-29 08:18:25 +05:00
//private string dir = @"/archive_rmt/data";
2020-09-04 12:49:15 +05:00
public FileClient()
{
TempDir.StartCkeckDir();
}
public FileClient(string directory)
{
TempDir.StartCkeckDir();
dir = directory;
}
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);
Stream fs = File.OpenRead(tmpFileName);
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)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return new byte[0];
}
finally
{
TempDir.RemoveProtect(name);
fs.Close();
}
}
private bool CheckArchive(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");
return (File.Exists(tmp + ".tar") || File.Exists(tmp + ".tar.gz"));
}
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)
{
Stream inStream = File.OpenRead(gzArchiveName);
Stream gzipStream = new GZipInputStream(inStream);
try
{
TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
tarArchive.ExtractContents(destFolder);
tarArchive.Close();
}
catch (Exception e)
{
Console.WriteLine("Can't extract: " + gzArchiveName);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally
{
gzipStream.Close();
inStream.Close();
}
}
private void ExtractTar(String tarFileName, String destFolder)
{
Stream inStream = File.OpenRead(tarFileName);
try
{
TarArchive tarArchive = TarArchive.CreateInputTarArchive(inStream);
tarArchive.ExtractContents(destFolder);
tarArchive.Close();
}
catch (Exception e)
{
Console.WriteLine("Can't extract: " + tarFileName);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
finally
{
inStream.Close();
}
}
}
}