Esempio n. 1
0
int main() {
	cout << "Welcome to CS106 Shrink-It(tm)!" << endl;
	cout << "This program uses the Huffman coding algorithm for compression." << endl;
	cout << "Any file can be compressed by this method, often with substantial" << endl;
	cout << "savings. Decompression will faithfully reproduce the original." << endl << endl;
	while (true){
		cout << "Do you want to compress or decompress a file? " << endl;
		string response = GetLine();
		if (response[0]== 'y' || response[0] == 'Y'){
			cout << "Are we compressing?" << endl;
			while (true){
				response = GetLine();
				if (response[0] == 'y' || response[0] == 'Y'){
					CompressFile();
					break;
				}
				else if (response[0] == 'n' || response[0] == 'N'){
					DecompressFile();
					break;
				}
				else 
					cout << "Please enter yes of no, it's not hard. " << endl;
			}
		}
		else if (response[0] == 'n' || response[0] == 'N'){
			break;
		}
	}


	cout << endl << "Thanks for using Shrink-It" << endl;
	return 0;
}
Esempio n. 2
0
static BROTLI_BOOL DecompressFiles(Context* context) {
  while (NextFile(context)) {
    BROTLI_BOOL is_ok = BROTLI_TRUE;
    BrotliDecoderState* s = BrotliDecoderCreateInstance(BrotliAllocFunc, BrotliFreeFunc, &scratch_buffer_size);
    if (!s) {
      fprintf(stderr, "out of memory\n");
      return BROTLI_FALSE;
    }
    /* This allows decoding "large-window" streams. Though it creates
       fragmentation (new builds decode streams that old builds don't),
       it is better from used experience perspective. */
    BrotliDecoderSetParameter(s, BROTLI_DECODER_PARAM_LARGE_WINDOW, 1u);
    is_ok = OpenFiles(context);
    if (is_ok && !context->current_input_path &&
        !context->force_overwrite && isatty(STDIN_FILENO)) {
      fprintf(stderr, "Use -h help. Use -f to force input from a terminal.\n");
      is_ok = BROTLI_FALSE;
    }
    if (is_ok) is_ok = DecompressFile(context, s);
    BrotliDecoderDestroyInstance(s);
    if (!CloseFiles(context, is_ok)) is_ok = BROTLI_FALSE;
    if (!is_ok) return BROTLI_FALSE;
  }
  return BROTLI_TRUE;
}
Esempio n. 3
0
bool CZlib::Decode(CArcFile* archive)
{
	const SFileInfo* file_info = archive->GetOpenFileInfo();

	if (file_info->format != _T("zlib"))
		return false;

	DecompressFile(archive);

	return true;
}
Esempio n. 4
0
bool ZIPArchive::ReadFile(){
	int fileHeader = mainStream.readType<int>();
	if (fileHeader != 0x04034b50) return false;

	int versionNeeded = mainStream.readType<short>();

	// Ignore 'general purpose bit flag'
	mainStream.readType<short>();

	int compressionMethod = mainStream.readType<short>();

	// Ignore modified file times.
	mainStream.readType<int>();

	//return true;
	int crc32 = mainStream.readType<int>();

	ZIPFileData* newFile = new ZIPFileData();
	newFile->compressedSize = mainStream.readType<int>();
	newFile->uncompressedSize = mainStream.readType<int>();

	int fileNameLength = mainStream.readType<short>();
	int extraFieldLength = mainStream.readType<short>();

	newFile->filepath = mainStream.readString(fileNameLength);
	std::string extraField = mainStream.readString(extraFieldLength);

	newFile->compressedData = mainStream.readString(newFile->compressedSize);

	switch (compressionMethod){
	case 0:
		newFile->decompressedData = newFile->compressedData;
		break;
	case 8: // Deflate
		DecompressFile(newFile);
		break;
	}

	readFiles.push_back(newFile);
	return true;
}
Esempio n. 5
0
int main(int argc,char**argv)
{
	if(argc<4)
	{
		Usage();
		return 1;
	}
	if(!stricmp(argv[1],"-c"))//压缩一个文件
	{
		FILE*fp=fopen(argv[2],"rb");
		FILE*fpout=fopen(argv[3],"wb");
		int iRet;
		unsigned long fLen;
		if(!fp)
		{
			fprintf(stderr,"Unable to open %s\n",argv[2]);
			return 2;
		}
		if(!fpout)
		{
			fprintf(stderr,"Unable to write %s\n",argv[3]);
			return 2;
		}
		fseek(fp,0,SEEK_END);
		fLen=ftell(fp);
		fseek(fp,0,SEEK_SET);
		printf("Input file size=%u\n",fLen);
		iRet=CompressFile(fpout,fp,fLen);
		if(iRet)
			fprintf(stderr,"Error:%d\n",iRet);
		fclose(fpout);
		fclose(fp);
		if(iRet)
			unlink(argv[3]);
		return iRet;
	}
	if(!stricmp(argv[1],"-d"))//解压一个文件
	{
		FILE*fp=fopen(argv[2],"rb");
		FILE*fpout=fopen(argv[3],"wb");
		int iRet;
		if(!fp)
		{
			fprintf(stderr,"Unable to open %s\n",argv[2]);
			return 2;
		}
		if(!fpout)
		{
			fprintf(stderr,"Unable to write %s\n",argv[3]);
			return 2;
		}
		iRet=DecompressFile(fpout,fp);
		if(iRet)
			fprintf(stderr,"Error:%d\n",iRet);
		fclose(fpout);
		fclose(fp);
		if(iRet)
			unlink(argv[3]);
		return iRet;
	}
	Usage();
	return 1;
}