31
31
/* Rua Comendador Francisco josé da Cunha, 111 - Itabaiana - SE - 49500-000 */
32
32
/********************************************************************************/
33
33
using System . IO ;
34
- using System . IO . Compression ;
35
- using System . Text ;
34
+ using System . Reflection ;
35
+ using System . Xml ;
36
+ using System . Xml . Serialization ;
37
+ using CTe . AppTeste . Entidades ;
36
38
37
- namespace NFe . Utils
39
+ namespace CTe . AppTeste . Dao
38
40
{
39
- public static class Compressao
41
+ public class ConfiguracaoDao
40
42
{
41
- private static void CopiarPara ( Stream src , Stream dest )
42
- {
43
- var bytes = new byte [ 4096 ] ;
44
-
45
- int cnt ;
43
+ private readonly string _caminhoAplicacao ;
44
+ private string _nomeArquivoXml = "Configuracao.xml" ;
46
45
47
- while ( ( cnt = src . Read ( bytes , 0 , bytes . Length ) ) != 0 )
48
- {
49
- dest . Write ( bytes , 0 , cnt ) ;
50
- }
46
+ public ConfiguracaoDao ( )
47
+ {
48
+ _caminhoAplicacao = Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ;
51
49
}
52
50
53
- /// <summary>
54
- /// Compacta uma string para GZip
55
- /// </summary>
56
- /// <param name="str"></param>
57
- /// <returns></returns>
58
- public static byte [ ] Zip ( string str )
51
+ public void SalvarConfiguracao ( Configuracao configuracao )
59
52
{
60
- var bytes = Encoding . UTF8 . GetBytes ( str ) ;
61
-
62
- using ( var msi = new MemoryStream ( bytes ) )
63
- using ( var mso = new MemoryStream ( ) )
53
+ using ( var stream = new StreamWriter ( _caminhoAplicacao + @"\" + _nomeArquivoXml ) )
64
54
{
65
- using ( var gs = new GZipStream ( mso , CompressionMode . Compress ) )
66
- {
67
- CopiarPara ( msi , gs ) ;
68
- }
55
+ var xmlSerializer = new XmlSerializer ( typeof ( Configuracao ) ) ;
69
56
70
- return mso . ToArray ( ) ;
57
+ xmlSerializer . Serialize ( XmlWriter . Create ( stream ) , configuracao ) ;
58
+
59
+ stream . Flush ( ) ;
71
60
}
72
61
}
73
62
74
- /// <summary>
75
- /// Descompacta uma string GZip
76
- /// </summary>
77
- /// <param name="bytes"></param>
78
- /// <returns></returns>
79
- public static string Unzip ( byte [ ] bytes )
63
+ public Configuracao BuscarConfiguracao ( )
80
64
{
81
- using ( var msi = new MemoryStream ( bytes ) )
82
- using ( var mso = new MemoryStream ( ) )
65
+ if ( ! File . Exists ( _caminhoAplicacao + @"\" + _nomeArquivoXml ) ) return null ;
66
+
67
+ Configuracao configuracao ;
68
+
69
+ using ( var reader = new StreamReader ( _caminhoAplicacao + @"\" + _nomeArquivoXml ) )
83
70
{
84
- using ( var gs = new GZipStream ( msi , CompressionMode . Decompress ) )
85
- {
86
- CopiarPara ( gs , mso ) ;
87
- }
71
+ var xmlSerializer = new XmlSerializer ( typeof ( Configuracao ) ) ;
88
72
89
- return Encoding . UTF8 . GetString ( mso . ToArray ( ) ) ;
73
+ var objeto = xmlSerializer . Deserialize ( XmlReader . Create ( reader ) ) ;
74
+
75
+ configuracao = objeto as Configuracao ;
90
76
}
91
- }
92
77
78
+ return configuracao ;
79
+ }
93
80
}
94
- }
81
+ }
0 commit comments