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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace сжимай
{
public partial class Form1 : Form
{
const byte AR_SIMPLE = 0;
const byte AR_REPEATED = 1;
public Form1()
{
InitializeComponent();
}
private void Archive(string InputFileName, string OutputFileName)
{
FileStream file_r = new FileStream(@InputFileName, FileMode.OpenOrCreate, FileAccess.Read);
pgBar.Maximum = (int)file_r.Length;
int curByte = file_r.ReadByte();
int nextByte = file_r.ReadByte();
curByte++;
//start from beggining
file_r.Position = 0;
do
{
if (nextByte == curByte)
{
//mark as repeadted and Archive
curByte = PackBlock1(file_r, OutputFileName);
}
else
{
//mark as NotRepeadted
curByte = PackBlock2(file_r, OutputFileName);
}
nextByte = file_r.ReadByte();
file_r.Position--;
// to app not "FREEZ"
pgBar.Value = (int)file_r.Position;
Application.DoEvents();
}
while (nextByte != -1);
file_r.Close();
}
private int PackBlock1(FileStream file_r, string FileName)
{
byte count = 1;
int curByte;
int nextByte;
byte[] buf;
FileStream file_w = new FileStream(@FileName, FileMode.Append, FileAccess.Write);
//Start seach & count cycle
do
{
count++;
curByte = file_r.ReadByte();
nextByte = file_r.ReadByte();
if (nextByte != -1)
file_r.Position--;
if (count == 255)
{ //write each 255 blocks
buf = new byte[3] { AR_REPEATED, count, (byte)curByte };
file_w.Write(buf, 0, 3);
//start from 1
count++;
}
}
while (curByte == nextByte);
//Mark block as repeadted
buf = new byte[3] { AR_REPEATED, count, (byte)curByte };
file_w.Write(buf, 0, 3);
file_w.Close();
return curByte;
}
private int PackBlock2(FileStream file_r, string FileName)
{
MemoryStream tmp = new MemoryStream();
int curByte;
int nextByte;
byte count = 1;
do
{
count++;
curByte = file_r.ReadByte();
nextByte = file_r.ReadByte();
if (nextByte != -1)
file_r.Position--;
if (curByte != nextByte)
tmp.WriteByte((byte)curByte);
}
while ((curByte != nextByte) && (tmp.Length < 255) && (nextByte != -1));
FileStream file_w = new FileStream(FileName, FileMode.Append, FileAccess.Write);
BinaryWriter sw = new BinaryWriter(file_w);
//Mark block as NOT repeadted
sw.Write(AR_SIMPLE);
//Write length
sw.Write((byte)tmp.Length);
//Write body
sw.Write(tmp.ToArray(), 0, (int)tmp.Length);
sw.Close();
file_w.Close();
return curByte;
}
private void unArchive(string InputFileName, string OutputFileName)
{
// stream: Read from arch
FileStream file_r = new FileStream(InputFileName, FileMode.Open, FileAccess.Read);
BinaryReader sr = new BinaryReader(file_r);
// stream: Write in
FileStream file_w = new FileStream(OutputFileName, FileMode.Append, FileAccess.Write);
BinaryWriter sw = new BinaryWriter(file_w);
pgBar.Maximum = (int)file_r.Length;
byte curByte;
byte count;
do
{
curByte = sr.ReadByte();
switch (curByte)
{
case AR_SIMPLE:
count = sr.ReadByte();
for (int i = 0; i < count; i++)
{
sw.Write(sr.ReadByte());
}
break;
case AR_REPEATED:
count = sr.ReadByte();
curByte = sr.ReadByte();
for (int i = 0; i < count; i++)
{
sw.Write(curByte); //write repeadted character
}
break;
}
// to app not "FREEZ"
pgBar.Value = (int)file_r.Position;
Application.DoEvents();
}
while (sr.PeekChar() != -1);
sr.Close();
file_r.Close();
sw.Close();
file_w.Close();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Select file to pack...";
openFileDialog1.Filter = "Any|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (File.Exists(openFileDialog1.FileName + ".tfArchive"))
{
File.Delete(openFileDialog1.FileName + ".tfArchive");
}
pgBar.Value = 0;
pgBar.Visible = true;
lbStatus.Text = "...";
Archive(openFileDialog1.FileName, openFileDialog1.FileName + ".tfArchive");
lbStatus.Text = "Packed";
pgBar.Visible = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Select file to unpack...";
openFileDialog1.Filter = "TFSoft Archive|*.TFArchive";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
saveFileDialog1.InitialDirectory = Path.GetDirectoryName(openFileDialog1.FileName);
saveFileDialog1.FileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
pgBar.Value = 0;
pgBar.Visible = true;
lbStatus.Text = "...";
unArchive(openFileDialog1.FileName, saveFileDialog1.FileName);
lbStatus.Text = "UnPacked";
pgBar.Visible = false;
}
}
}
}
} |