-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.cpp
180 lines (149 loc) · 3.74 KB
/
pack.cpp
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
#include "pack.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
// DEBUG
// using namespace std;
using std::string;
using std::ifstream;
using std::ofstream;
using std::ios;
string Jidantou::IntToString(int64_t val)
{
int length = 0;
int i = 0, j;
string str = "";
char temp;
if(!val)
return "0";
if (val < 0)
{
val = -val;
str = "-";
i = 1;
}
// put ever number into str
while(val)
{
str = str + (char)(val % 10 + 48);
val = val / 10;
length++;
}
// change the order in str
for(j = str.length() - 1; i < j; i++, j--)
{
temp = str[j];
str[j] = str[i];
str[i] = temp;
}
return str;
}
/*
static char DecimalString[11] = "0123456789";
// turn int to std::string
string Jidantou::IntToString(int64_t integer)
{
string finalstr;
char str[20] = {"0000000000000000000"};
int i = 19, j;
while (integer && i)
{
str[i] = DecimalString[integer % 10];
integer /= 10;
i--;
}
for(i = 0; str[i] == '0' && i < 20; i++);
std::cout << i << std::endl;
if(i == 20)
finalstr = "0";
else if(i == 0)
finalstr = str;
else
{
char * s = new char[20 - i];
for(j = 0; i < 20; i++, j++)
{
s[j] = str[i];
}
finalstr = s;
delete[] s;
}
return finalstr;
}
*/
// pack file into several files and entrypt
int Jidantou::PackFile(string fileName, int size)
{
unsigned long fileSize;
char * pbuff;
int buffNum;
string outputPath;
ifstream * pfile = new ifstream(fileName, ios::binary | ios::in);
ofstream * pOutputFile;
if(pfile->fail())
return 1;
if(fileName.find('.') == string::npos)
return 3;
fileName.erase(fileName.find_last_of('.'), fileName.length());
if(system(("mkdir " + fileName).c_str()) == -1)
return 2;
// get the size of file
pfile->seekg(0, ios::end);
fileSize = pfile->tellg();
pfile->seekg(0, ios::beg);
// DEBUG
// cout << fileSize << endl;
// get the number of output files
buffNum = fileSize / size;
// DEBUG
// cout << "buffNum:" << buffNum << endl;
for (int i = 0; i < buffNum; i++)
{
outputPath = ".\\" + fileName + "\\" + fileName + IntToString(i);
pOutputFile = new ofstream(outputPath, ios::binary);
if(!pOutputFile->fail())
{
pbuff = new char[size];
pfile->read(pbuff, size);
// deal with data in the file
pOutputFile->write(pbuff, size);
pOutputFile->close();
delete [] pbuff;
}
else
{
// deal with error in output files
}
delete pOutputFile;
}
// DEBUG
// if(pfile->eof())
// cout << "pfile->eof()" << endl;
if(fileSize % size)
{
if(!pfile->eof())
{
outputPath = ".\\" + fileName + "\\" + fileName + IntToString(buffNum);
pOutputFile = new ofstream(outputPath, ios::binary);
if(!pOutputFile->fail())
{
pbuff = new char[fileSize - buffNum * size];
pfile->read(pbuff, fileSize - buffNum * size);
// deal with data in the file
pOutputFile->write(pbuff, fileSize - buffNum * size);
pOutputFile->close();
delete [] pbuff;
}
else
{
// deal with error
// in the last output file
}
buffNum++;
delete pOutputFile;
}
}
pfile->close();
delete pfile;
return 0;
}