markus / GzipTest / MainWindow.xaml.cs @ 787a4489
이력 | 보기 | 이력해설 | 다운로드 (3.76 KB)
1 | 787a4489 | KangIngu | using ICSharpCode.SharpZipLib.Zip; |
---|---|---|---|
2 | using System; |
||
3 | using System.Collections.Generic; |
||
4 | using System.IO; |
||
5 | using System.IO.Compression; |
||
6 | using System.Linq; |
||
7 | using System.Text; |
||
8 | using System.Windows; |
||
9 | using System.Windows.Controls; |
||
10 | using System.Windows.Data; |
||
11 | using System.Windows.Documents; |
||
12 | using System.Windows.Input; |
||
13 | using System.Windows.Media; |
||
14 | using System.Windows.Media.Imaging; |
||
15 | using System.Windows.Navigation; |
||
16 | using System.Windows.Shapes; |
||
17 | |||
18 | namespace GzipTest |
||
19 | { |
||
20 | /// <summary> |
||
21 | /// Interaction logic for MainWindow.xaml |
||
22 | /// </summary> |
||
23 | public partial class MainWindow : Window |
||
24 | { |
||
25 | public MainWindow() |
||
26 | { |
||
27 | InitializeComponent(); |
||
28 | this.Loaded += MainWindow_Loaded; |
||
29 | } |
||
30 | |||
31 | private void MainWindow_Loaded(object sender, RoutedEventArgs e) |
||
32 | { |
||
33 | string text = System.IO.File.ReadAllText(@"F:\Project_POC\KCOM\GzipTest\json.txt"); |
||
34 | //var result = compress(text); |
||
35 | |||
36 | var result = compress(text); |
||
37 | |||
38 | var result2 = decompress(result); |
||
39 | } |
||
40 | |||
41 | public static string compress(string str) |
||
42 | { |
||
43 | using (MemoryStream outStream = new MemoryStream()) |
||
44 | { |
||
45 | using (GZipStream gzipStream = new GZipStream(outStream, CompressionMode.Compress)) |
||
46 | using (MemoryStream srcStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(str))) |
||
47 | srcStream.CopyTo(gzipStream); |
||
48 | |||
49 | return Convert.ToBase64String(outStream.ToArray()); |
||
50 | //return outStream.ToArray(); |
||
51 | } |
||
52 | } |
||
53 | public static string decompress(string data) |
||
54 | { |
||
55 | |||
56 | byte[] compressed = Convert.FromBase64String(data); |
||
57 | using (MemoryStream inStream = new MemoryStream(compressed)) |
||
58 | using (GZipStream gzipStream = new GZipStream(inStream, CompressionMode.Decompress)) |
||
59 | using (MemoryStream outStream = new MemoryStream()) |
||
60 | { |
||
61 | gzipStream.CopyTo(outStream); |
||
62 | return Encoding.ASCII.GetString(outStream.ToArray()); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | |||
67 | |||
68 | private String compress_NEW2(byte[] data) |
||
69 | { |
||
70 | String result = ""; |
||
71 | |||
72 | MemoryStream bos = new MemoryStream(); |
||
73 | ZipOutputStream zos = new ZipOutputStream(bos); |
||
74 | //GZIPOutputStream zos = null; |
||
75 | //try |
||
76 | //{ |
||
77 | // zos = new GZIPOutputStream(bos); |
||
78 | //} |
||
79 | //catch (IOException e) |
||
80 | //{ |
||
81 | // e.printStackTrace(); |
||
82 | //} |
||
83 | ZipEntry entry = new ZipEntry("test"); |
||
84 | zos.SetLevel(9); //0-9, 9 being the highest level of compression |
||
85 | zos.PutNextEntry(entry); |
||
86 | byte[] buffer = new byte[4096]; |
||
87 | zos.Write(data, 0, data.Length); |
||
88 | zos.Finish(); |
||
89 | zos.Close(); |
||
90 | |||
91 | return Convert.ToBase64String(bos.ToArray()); |
||
92 | } |
||
93 | |||
94 | private string decompress_NEW2(string data) |
||
95 | { |
||
96 | MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data)); |
||
97 | ZipInputStream input = new ZipInputStream(memoryStream); |
||
98 | ZipEntry zipEntry = input.GetNextEntry(); |
||
99 | while (zipEntry != null) |
||
100 | { |
||
101 | String entryFileName = zipEntry.Name; |
||
102 | // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); |
||
103 | // Optionally match entrynames against a selection list here to skip as desired. |
||
104 | // The unpacked length is available in the zipEntry.Size property. |
||
105 | |||
106 | byte[] buffer = new byte[4096]; // 4K is optimum |
||
107 | StreamReader reader = new StreamReader(input); |
||
108 | |||
109 | return reader.ReadToEnd(); |
||
110 | } |
||
111 | |||
112 | return null; |
||
113 | } |
||
114 | |||
115 | |||
116 | } |
||
117 | } |