-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHelper.cs
More file actions
80 lines (74 loc) · 2.76 KB
/
FileHelper.cs
File metadata and controls
80 lines (74 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using Lib_Sbis.Document;
using System;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Xml.Serialization;
namespace Lib_Sbis
{
public class FileHelper
{
public void ExtractXMLFromArchives()//exeption writer suda
{
string Dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
FileInfo[] zipsInfo = GetFilesInfoArray();
for (int i = 0; i < zipsInfo.Length; i++)
{
try
{
GetXMLFromZip(zipsInfo[i].FullName, Dir + @"\xml\" + zipsInfo[i].Name);
}
catch (Exception ex)
{
StreamWriter sw = new StreamWriter(Dir + @"\errorlog.txt", true);
string time = DateTime.Now.ToString();
sw.WriteLine(time+" "+ex.Message);
sw.Close();
}
}
}
public FileInfo[] GetFilesInfoArray(string extension=null)
{
if (extension == null)
extension = "zip";
string Dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\"+extension;
DirectoryInfo directoryInfo = new DirectoryInfo(Dir);
FileInfo[] files = directoryInfo.GetFiles("*."+extension);
return files;
}
public DirectoryInfo[] GetDirectoryInfoArray(string extension=null)
{
if (extension == null)
extension = "zip";
string Dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)+@"\"+extension;
DirectoryInfo directoryInfo = new DirectoryInfo(Dir);
DirectoryInfo[] directories = directoryInfo.GetDirectories();
return directories;
}
void GetXMLFromZip(string pathin,string pathout)
{
Directory.CreateDirectory(pathout);
using (var zip = ZipFile.Open(pathin,ZipArchiveMode.Read))
{
int i = 0; ;
foreach (var entry in zip.Entries)
{
if (entry.Name.EndsWith(".xml") && entry.Name.StartsWith("ON_NSCHFDOPPR"))
{
entry.ExtractToFile(pathout+@"\doc"+i+".xml");
}
i++;
}
}
}
public Object DeserializeXML(string path, Type type)
{
XmlSerializer xmlSerializer = new XmlSerializer(type);
using (FileStream fs = new FileStream(path, FileMode.Open))
{
var doc = xmlSerializer.Deserialize(fs);
return doc;
}
}
}
}