Skip to content

Commit 68c9b5b

Browse files
authored
[CORE] Add compress functionality to the Compress tool (#702)
1 parent e53e4d2 commit 68c9b5b

File tree

1 file changed

+71
-10
lines changed

1 file changed

+71
-10
lines changed

Core/Tools/Compress/Compress.cpp

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,22 @@ int main(int argc, char **argv)
9595
DEBUG_LOG(("IN:'%s' OUT:'%s' Compression:'%s'\n",
9696
inFile.c_str(), outFile.c_str(), CompressionManager::getCompressionNameByType(compressType)));
9797

98+
// just check compression on the input file if we have no output specified
9899
if (outFile.empty())
99100
{
100-
// just check compression
101-
FILE *fp = fopen(inFile.c_str(), "rb");
102-
if (!fp)
101+
FILE *fpIn = fopen(inFile.c_str(), "rb");
102+
if (!fpIn)
103103
{
104104
DEBUG_LOG(("Cannot open '%s'\n", inFile.c_str()));
105105
return EXIT_FAILURE;
106106
}
107-
fseek(fp, 0, SEEK_END);
108-
int size = ftell(fp);
109-
fseek(fp, 0, SEEK_SET);
107+
fseek(fpIn, 0, SEEK_END);
108+
int size = ftell(fpIn);
109+
fseek(fpIn, 0, SEEK_SET);
110110

111111
char data[8];
112-
int numRead = fread(data, 1, 8, fp);
112+
int numRead = fread(data, 1, 8, fpIn);
113+
fclose(fpIn);
113114

114115
if (numRead != 8)
115116
{
@@ -121,7 +122,7 @@ int main(int argc, char **argv)
121122
if (usedType == COMPRESSION_NONE)
122123
{
123124
DEBUG_LOG(("No compression on '%s'\n", inFile.c_str()));
124-
return EXIT_FAILURE;
125+
return EXIT_SUCCESS;
125126
}
126127

127128
int uncompressedSize = CompressionManager::getUncompressedSize(data, 8);
@@ -133,6 +134,66 @@ int main(int argc, char **argv)
133134
return EXIT_SUCCESS;
134135
}
135136

136-
// compress file
137-
return EXIT_FAILURE;
137+
// Open the input file
138+
FILE *fpIn = fopen(inFile.c_str(), "rb");
139+
if (!fpIn)
140+
{
141+
DEBUG_LOG(("Cannot open input '%s'\n", inFile.c_str()));
142+
return EXIT_FAILURE;
143+
}
144+
145+
// Read the input file
146+
fseek(fpIn, 0, SEEK_END);
147+
int inputSize = ftell(fpIn);
148+
fseek(fpIn, 0, SEEK_SET);
149+
150+
char *inputData = new char[inputSize];
151+
int numRead = fread(inputData, 1, inputSize, fpIn);
152+
fclose(fpIn);
153+
if (numRead != inputSize)
154+
{
155+
DEBUG_LOG(("Cannot read input '%s'\n", inFile.c_str()));
156+
delete[] inputData;
157+
return EXIT_FAILURE;
158+
}
159+
160+
DEBUG_LOG(("Read %d bytes from '%s'\n", numRead, inFile.c_str()));
161+
162+
// Open the output file
163+
FILE *fpOut = fopen(outFile.c_str(), "wb");
164+
if (!fpOut)
165+
{
166+
DEBUG_LOG(("Cannot open output '%s'\n", outFile.c_str()));
167+
delete[] inputData;
168+
return EXIT_FAILURE;
169+
}
170+
171+
172+
if (compressType == COMPRESSION_NONE)
173+
{
174+
DEBUG_LOG(("No compression requested, writing uncompressed data\n"));
175+
int outSize = CompressionManager::getUncompressedSize(inputData, inputSize);
176+
char *outData = new char[outSize];
177+
CompressionManager::decompressData(inputData, inputSize, outData, outSize);
178+
179+
// Write the output file
180+
fwrite(outData, 1, outSize, fpOut);
181+
}
182+
else
183+
{
184+
DEBUG_LOG(("Compressing data using %s\n", CompressionManager::getCompressionNameByType(compressType)));
185+
// Allocate the output buffer
186+
int outSize = CompressionManager::getMaxCompressedSize(inputSize, compressType);
187+
char *outData = new char[outSize];
188+
int compressedSize = CompressionManager::compressData(compressType, inputData, inputSize, outData, outSize);
189+
190+
// Write the output file
191+
fwrite(outData, 1, compressedSize, fpOut);
192+
delete[] outData;
193+
}
194+
195+
fclose(fpOut);
196+
delete[] inputData;
197+
198+
return EXIT_SUCCESS;
138199
}

0 commit comments

Comments
 (0)