Example #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;
}
Example #2
0
void SaveSurface(std::string filename, const ImageData& imageData)
{
    std::vector<unsigned char> output;
    int result = CompressFile(imageData.bytes, output);
    if (result != 0) {
        std::cout << "Compression error occurred" << std::endl;
        exit(1);
    }

    SaveData(filename, output);
}
Example #3
0
bool PrepDumpForUpload(gcString &dumpFile)
{
	UTIL::FS::Path path(dumpFile, "", true);

	if (!UTIL::FS::isValidFile(path))
		return false;

	if (Safe::stricmp(path.getFile().getFileExt().c_str(),"dmp") == 0)
		CompressFile(dumpFile);

	return true;
}
Example #4
0
static BROTLI_BOOL CompressFiles(Context* context) {
  while (NextFile(context)) {
    BROTLI_BOOL is_ok = BROTLI_TRUE;
    BrotliEncoderState* s = BrotliEncoderCreateInstance(NULL, NULL, NULL);
    if (!s) {
      fprintf(stderr, "out of memory\n");
      return BROTLI_FALSE;
    }
    BrotliEncoderSetParameter(s,
        BROTLI_PARAM_QUALITY, (uint32_t)context->quality);
    if (context->lgwin > 0) {
      /* Specified by user. */
      /* Do not enable "large-window" extension, if not required. */
      if (context->lgwin > BROTLI_MAX_WINDOW_BITS) {
        BrotliEncoderSetParameter(s, BROTLI_PARAM_LARGE_WINDOW, 1u);
      }
      BrotliEncoderSetParameter(s,
          BROTLI_PARAM_LGWIN, (uint32_t)context->lgwin);
    } else {
      /* 0, or not specified by user; could be chosen by compressor. */
      uint32_t lgwin = DEFAULT_LGWIN;
      /* Use file size to limit lgwin. */
      if (context->input_file_length >= 0) {
        int32_t size = 1 << BROTLI_MIN_WINDOW_BITS;
        lgwin = BROTLI_MIN_WINDOW_BITS;
        while (size < context->input_file_length) {
          size <<= 1;
          lgwin++;
          if (lgwin == BROTLI_MAX_WINDOW_BITS) break;
        }
      }
      BrotliEncoderSetParameter(s, BROTLI_PARAM_LGWIN, lgwin);
    }
    if (context->input_file_length > 0) {
      uint32_t size_hint = context->input_file_length < (1 << 30) ?
          (uint32_t)context->input_file_length : (1u << 30);
      BrotliEncoderSetParameter(s, BROTLI_PARAM_SIZE_HINT, size_hint);
    }
    is_ok = OpenFiles(context);
    if (is_ok && !context->current_output_path &&
        !context->force_overwrite && isatty(STDOUT_FILENO)) {
      fprintf(stderr, "Use -h help. Use -f to force output to a terminal.\n");
      is_ok = BROTLI_FALSE;
    }
    if (is_ok) is_ok = CompressFile(context, s);
    BrotliEncoderDestroyInstance(s);
    if (!CloseFiles(context, is_ok)) is_ok = BROTLI_FALSE;
    if (!is_ok) return BROTLI_FALSE;
  }
  return BROTLI_TRUE;
}
Example #5
0
static PyObject *zopfli_compress_file(PyObject *self, PyObject *args) {
    const char *infilename;
    const char *outfilename;
    if (!PyArg_ParseTuple(args, "ss", &infilename, &outfilename)) {
        return NULL;
    }
    Options options;
    InitOptions(&options);
    OutputType output_type = OUTPUT_GZIP;
    options.numiterations = 5;
    CompressFile(&options, output_type, infilename, outfilename);
    Py_INCREF(Py_None);
    return Py_None;
}
bool PrepDumpForUpload(gcString &dumpFile)
{
	UTIL::FS::Path path(dumpFile, "", true);

	if (!UTIL::FS::isValidFile(path))
		return false;

	auto strExt = path.getFile().getFileExt();

	if (strExt == "dmp" || strExt == "log")
		CompressFile(dumpFile);

	return true;
}
Example #7
0
int main(int argc, char* argv[])
{
  TCHAR path[MAX_PATH];
  TCHAR filename[MAX_PATH];
  TCHAR zip_filename[MAX_PATH];
  TCHAR unzip_path[MAX_PATH];
  size_t i;
  size_t len;

  GetModuleFileName(0, path, MAX_PATH);
  len = _tcslen(path);

  for (i = len - 1; i >= 0; --i) {
    if (path[i] == _T('\\') || path[i] == _T('/')) {
      path[i] = _T('\0');
      break;
    }
  }
  
  printf("%ls\n", path);

  if (!InitZipUtils(path))
    printf("Initilization the zip dynamic library failed.\n");
  else
    printf("Initilization the zip dynamic library success.\n");


  //file
  _stprintf(filename,    _T("%s/ZipUtilsApp.c"), path);
  _stprintf(zip_filename,_T("%s/ZipUtilsApp.zip"), path);


  CompressFile(filename, zip_filename);

  //folder
  _stprintf(zip_filename, _T("%s.zip"), path);
  CompressFolder(path, zip_filename);

  //unzip
  _stprintf(unzip_path, _T("%s/unzip"), path);
  UnCompressFile(zip_filename, unzip_path);


  UnInitZipUtils();

  getch();

  return 0;
}
Example #8
0
bool CXmlStreamWrite::EndXmlFile()
{
	if (fp){
		if (AnyToWrite) fprintf(fp,"%s", ToWrite.c_str()); //write any remaining part of the last tag...
		fclose(fp);
	}
	else {LastError += "Invalid file pointer encountered on saving XML. Aborting.\n"; return false;}

	if (WantCompressed){
		if (CompressFile(filename, filename+".tmp", &LastError)){
			remove((filename+".tmp").c_str()); //delete the temp file
			return true;
		}
		else { //if no commpression (IE on a platform with zip compress not yet implemented) just use the ascii version
			LastError += "Could not compress AMF. Saved uncompressed version instead.\n";
			rename((filename+".tmp").c_str(), filename.c_str());
			return false;
		}
	}
	return true;
}
Example #9
0
int zopfli_convert(const char *in_file, const char *out_file, const ZopfliConfig *zopfli_config) {
  ZopfliOptions options;
  ZopfliFormat output_type = ZOPFLI_FORMAT_GZIP;

  ZopfliInitOptions(&options);

  if (zopfli_config->type == 1) {
    output_type = ZOPFLI_FORMAT_DEFLATE;
  } else if (zopfli_config->type == 2) {
    output_type = ZOPFLI_FORMAT_GZIP;
  } else {
    output_type = DEFAULT_FORMAT;
  }

  if (zopfli_config->numiterations > 0) {
    options.numiterations = zopfli_config->numiterations;
  }

  CompressFile(&options, output_type, in_file, out_file);

  return 0;
}
Example #10
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;
}
Example #11
0
int main(int argc, char* argv[]) {
  ZopfliOptions options;
  ZopfliFormat output_type = ZOPFLI_FORMAT_GZIP;
  const char* filename = 0;
  int output_to_stdout = 0;
  int i;

  ZopfliInitOptions(&options);

  for (i = 1; i < argc; i++) {
    const char* arg = argv[i];
    if (StringsEqual(arg, "-v")) options.verbose = 1;
    else if (StringsEqual(arg, "-c")) output_to_stdout = 1;
    else if (StringsEqual(arg, "--deflate")) {
      output_type = ZOPFLI_FORMAT_DEFLATE;
    }
    else if (StringsEqual(arg, "--zlib")) output_type = ZOPFLI_FORMAT_ZLIB;
    else if (StringsEqual(arg, "--gzip")) output_type = ZOPFLI_FORMAT_GZIP;
    else if (StringsEqual(arg, "--splitlast")) options.blocksplittinglast = 1;
    else if (arg[0] == '-' && arg[1] == '-' && arg[2] == 'i'
        && arg[3] >= '0' && arg[3] <= '9') {
      options.numiterations = atoi(arg + 3);
    }
    else if (StringsEqual(arg, "-h")) {
      fprintf(stderr,
          "Usage: zopfli [OPTION]... FILE...\n"
          "  -h    gives this help\n"
          "  -c    write the result on standard output, instead of disk"
          " filename + '.gz'\n"
          "  -v    verbose mode\n"
          "  --i#  perform # iterations (default 15). More gives"
          " more compression but is slower."
          " Examples: --i10, --i50, --i1000\n");
      fprintf(stderr,
          "  --gzip        output to gzip format (default)\n"
          "  --zlib        output to zlib format instead of gzip\n"
          "  --deflate     output to deflate format instead of gzip\n"
          "  --splitlast   do block splitting last instead of first\n");
      return 0;
    }
  }

  if (options.numiterations < 1) {
    fprintf(stderr, "Error: must have 1 or more iterations");
    return 0;
  }

  for (i = 1; i < argc; i++) {
    if (argv[i][0] != '-') {
      char* outfilename;
      filename = argv[i];
      if (output_to_stdout) {
        outfilename = 0;
      } else if (output_type == ZOPFLI_FORMAT_GZIP) {
        outfilename = AddStrings(filename, ".gz");
      } else if (output_type == ZOPFLI_FORMAT_ZLIB) {
        outfilename = AddStrings(filename, ".zlib");
      } else {
        assert(output_type == ZOPFLI_FORMAT_DEFLATE);
        outfilename = AddStrings(filename, ".deflate");
      }
      if (options.verbose && outfilename) {
        fprintf(stderr, "Saving to: %s\n", outfilename);
      }
      CompressFile(&options, output_type, filename, outfilename);
      free(outfilename);
    }
  }

  if (!filename) {
    fprintf(stderr,
            "Please provide filename\nFor help, type: %s -h\n", argv[0]);
  }

  return 0;
}
Example #12
0
BOOL slimhelper::CompressDirectory(const CString& strDirectory, 
                                   ISystemSlimCallBack* piCallback,
                                   int nLevel)
{
    BOOL retval = FALSE;
    CString strPath;
    CString strFind;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA findData;
    BOOL bRetCode;
    BOOL bContinue;

    if (strDirectory.IsEmpty() || !piCallback)
        goto clean0;

    strPath = strDirectory;
    if (strPath[strPath.GetLength() - 1] != _T('\\'))
        strPath += _T("\\");

    strFind = strPath + _T("*.*");

    hFind = ::FindFirstFile(strFind, &findData); 
    if (INVALID_HANDLE_VALUE == hFind)
    {
        retval = TRUE;
        goto clean0;
    }

    bRetCode = TRUE;
    while (bRetCode)
    {
        if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)  
        {
            if (_tcscmp(findData.cFileName, _T(".")) &&
                _tcscmp(findData.cFileName, _T("..")))
            {
                CString strSubDir = strPath + findData.cFileName;
                strSubDir += _T("\\");
                if (!CompressDirectory(strSubDir, piCallback, nLevel + 1))
                        goto clean0;
            }
        }
        else
        {
            CString strFullFilePath = strPath + findData.cFileName;
            ULONGLONG qwFileSize = ((ULONGLONG)findData.nFileSizeHigh << 32) + findData.nFileSizeLow;
            
            CompressFile(strFullFilePath, qwFileSize, findData.dwFileAttributes, piCallback);

            bContinue = piCallback->OnIdle();
            if (!bContinue)
                goto clean0;
        }

        bRetCode = ::FindNextFile(hFind, &findData);
    }

    retval = TRUE;

clean0:
    if (hFind != INVALID_HANDLE_VALUE)
    {
        ::FindClose(hFind);
        hFind = INVALID_HANDLE_VALUE;
    }

    if (0 == nLevel)
    {
        CompressFile(strDirectory, 4, INVALID_FILE_ATTRIBUTES, piCallback);
    }

    return retval;
}