/* =========================================================================== * Compress the given file: create a corresponding .gz file and remove the * original. */ void file_compress(char *file, char *mode) { char outfile[MAX_NAME_LEN]; FILE *in; gzFile out; strcpy(outfile, file); strcat(outfile, ".gz"); in = fopen(file, "rb"); if (in == NULL) { char msgBuf[2048]; sprintf(msgBuf, "ERROR: Unable to send file. %s %s", strerror(errno), file); AfxMessageBox(msgBuf); return; } out = gzopen(outfile, mode); if (out == NULL) { AfxMessageBox("ERROR: unable to make gzip compression"); return; } gz_compress(in, out); }
/* =========================================================================== * Compress the given file: create a corresponding .gz file and remove the * original. */ void file_compress( char *file, char *mode) { local char outfile[MAX_NAME_LEN]; FILE *in; gzFile out; if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) { fprintf(stderr, "%s: filename too long\n", prog); exit(1); } strcpy(outfile, file); strcat(outfile, GZ_SUFFIX); in = fopen(file, "rb"); if (in == NULL) { perror(file); exit(1); } out = gzopen(outfile, mode); if (out == NULL) { fprintf(stderr, "%s: can't gzopen %s\n", prog, outfile); exit(1); } gz_compress(in, out); unlink(file); }
/** * Compress the given file: create a temporary output file. Remove original * file and rename temporary file after successfull completion of gz_compress(). * * @param lpsInfile * Pointer to filename of input file * @param lpsMode * Mode of file compression. ("wb[0-9]") * @return <code>TRUE</code> if compression succeeded, <code>FALSE</code> otherwise. */ BOOL dlp_fzip(const char *lpsInfile, const char *lpsMode) { #ifndef __NOZLIB char lpsFile[L_PATH]; char lpsDir[L_PATH]; char *lpsOutfile = NULL; BOOL bSuccess = TRUE; FILE *lpInfile; gzFile lpOutfile; /* Open input file */ lpInfile = fopen(lpsInfile, "rb"); if (lpInfile == NULL) { perror(lpsInfile); return FALSE; } /* Create and open temporary output file */ dlp_splitpath(lpsInfile,lpsDir,lpsFile); lpsOutfile = dlp_tempnam(NULL,lpsFile); lpOutfile = gzopen(lpsOutfile, lpsMode); if (lpOutfile == NULL) { fprintf(stderr, "zlib@zlib_fcompress(): can't gzopen %s\n", lpsOutfile); fclose(lpInfile); return FALSE; } bSuccess = gz_compress(lpInfile, lpOutfile); if(bSuccess) { strncpy(lpsFile,lpsInfile,L_PATH-2); strcat(lpsFile,"~"); rename(lpsInfile,lpsFile); if(gz_rename(lpsOutfile,lpsInfile)==0) { if(unlink(lpsFile)) perror("unlink"); } else { fprintf(stderr, "zlib@zlib_fcompress(): failed to compress file. Save %s uncompressed\n", lpsInfile); unlink(lpsInfile); rename(lpsFile,lpsInfile); } } else { fprintf(stderr, "zlib@zlib_fcompress(): failed to compress file. Save %s uncompressed\n", lpsInfile); if(unlink(lpsOutfile)) perror("unlink"); } return TRUE; #else /* __NOZLIB */ return FALSE; #endif }
unsigned char* gz_compress (unsigned char *src, unsigned long slen, long& dest_size, int level) { #define GZIP_HEADER_SIZE 256 unsigned char *dest = new unsigned char [compressBound (slen) + GZIP_HEADER_SIZE]; if ((dest_size = gz_compress (src, slen, dest, level)) < 0) { delete[] dest; return nullptr; } return dest; }
static void handle_stdout(void) { off_t gsize, usize; struct stat sb; time_t systime; uint32_t mtime; int ret; #ifndef SMALL if (fflag == 0 && isatty(STDOUT_FILENO)) { maybe_warnx("standard output is a terminal -- ignoring"); return; } #endif /* If stdin is a file use its mtime, otherwise use current time */ ret = fstat(STDIN_FILENO, &sb); #ifndef SMALL if (ret < 0) { maybe_warn("Can't stat stdin"); return; } #endif if (S_ISREG(sb.st_mode)) mtime = (uint32_t)sb.st_mtime; else { systime = time(NULL); #ifndef SMALL if (systime == -1) { maybe_warn("time"); return; } #endif mtime = (uint32_t)systime; } usize = gz_compress(STDIN_FILENO, STDOUT_FILENO, &gsize, "", mtime); #ifndef SMALL if (vflag && !tflag && usize != -1 && gsize != -1) print_verbage(NULL, NULL, usize, gsize); #endif }
/* =========================================================================== * Compress the given file: create a corresponding .gz file and remove the * original. @2 */ short file_compress(const char *szDir, const char *szFileName, const char *szSrcExt, const char *szZipExt, const char *mode) { char szSourceFile[MAX_NAME_LEN], szGzipFile[MAX_NAME_LEN]; FILE *in; gzFile out; if (strlen(szDir)) strcpy (szSourceFile, szDir); else strcpy (szSourceFile, "."); strcat (szSourceFile, "\\"); strcat (szSourceFile, szFileName); strcpy (szGzipFile, szSourceFile); strcat (szSourceFile, szSrcExt); strcat (szGzipFile, szZipExt); in = fopen(szSourceFile, "rb"); if (in == NULL) { perror(szSourceFile); return 1; } out = gzopen(szGzipFile, mode); if (out == NULL) { fclose(in); //@4 String msg = String ( "can't gzopen " ) + szGzipFile; throw std::exception( msg.c_str() ); return 1; } gz_compress(in, out); _unlink(szSourceFile); fclose(in); //@4 return 0; }
/* * compress the given file: create a corresponding .gz file and remove the * original. */ static off_t file_compress(char *file, char *outfile, size_t outsize) { int in; int out; off_t size, insize; #ifndef SMALL struct stat isb, osb; const suffixes_t *suff; #endif in = open(file, O_RDONLY); if (in == -1) { maybe_warn("can't open %s", file); return -1; } if (cflag == 0) { #ifndef SMALL if (fstat(in, &isb) == 0) { if (isb.st_nlink > 1 && fflag == 0) { maybe_warnx("%s has %d other link%s -- " "skipping", file, isb.st_nlink - 1, isb.st_nlink == 1 ? "" : "s"); close(in); return -1; } } if (fflag == 0 && (suff = check_suffix(file, 0)) && suff->zipped[0] != 0) { maybe_warnx("%s already has %s suffix -- unchanged", file, suff->zipped); close(in); return -1; } #endif /* Add (usually) .gz to filename */ if ((size_t)snprintf(outfile, outsize, "%s%s", file, suffixes[0].zipped) >= outsize) memcpy(outfile + outsize - suffixes[0].ziplen - 1, suffixes[0].zipped, suffixes[0].ziplen + 1); #ifndef SMALL if (check_outfile(outfile) == 0) { close(in); return -1; } #endif } if (cflag == 0) { out = open(outfile, O_WRONLY | O_CREAT | O_EXCL, 0600); if (out == -1) { maybe_warn("could not create output: %s", outfile); fclose(stdin); return -1; } } else out = STDOUT_FILENO; insize = gz_compress(in, out, &size, basename(file), (uint32_t)isb.st_mtime); (void)close(in); /* * If there was an error, insize will be -1. * If we compressed to stdout, just return the size. * Otherwise stat the file and check it is the correct size. * We only blow away the file if we can stat the output and it * has the expected size. */ if (cflag != 0) return insize == -1 ? -1 : size; #ifndef SMALL if (fstat(out, &osb) != 0) { maybe_warn("couldn't stat: %s", outfile); goto bad_outfile; } if (osb.st_size != size) { maybe_warnx("output file: %s wrong size (%" PRIdOFF " != %" PRIdOFF "), deleting", outfile, osb.st_size, size); goto bad_outfile; } copymodes(out, &isb, outfile); #endif if (close(out) == -1) maybe_warn("couldn't close output"); /* output is good, ok to delete input */ unlink_input(file, &isb); return size; #ifndef SMALL bad_outfile: if (close(out) == -1) maybe_warn("couldn't close output"); maybe_warnx("leaving original %s", file); unlink(outfile); return size; #endif }
int main() { __soaap_create_persistent_sandbox("compress"); some_flag = 1; gz_compress(0, 1); return 0; }
int main( int argc, char *argv[]) { int copyout = 0; int uncompr = 0; gzFile file; char *bname, outmode[20]; strcpy(outmode, "wb6 "); prog = argv[0]; bname = strrchr(argv[0], '/'); if (bname) bname++; else bname = argv[0]; argc--, argv++; if (!strcmp(bname, "gunzip")) uncompr = 1; else if (!strcmp(bname, "zcat")) copyout = uncompr = 1; while (argc > 0) { if (strcmp(*argv, "-c") == 0) copyout = 1; else if (strcmp(*argv, "-d") == 0) uncompr = 1; else if (strcmp(*argv, "-f") == 0) outmode[3] = 'f'; else if (strcmp(*argv, "-h") == 0) outmode[3] = 'h'; else if (strcmp(*argv, "-r") == 0) outmode[3] = 'R'; else if ((*argv)[0] == '-' && (*argv)[1] >= '1' && (*argv)[1] <= '9' && (*argv)[2] == 0) outmode[2] = (*argv)[1]; else break; argc--, argv++; } if (outmode[3] == ' ') outmode[3] = 0; if (argc == 0) { SET_BINARY_MODE(stdin); SET_BINARY_MODE(stdout); if (uncompr) { file = gzdopen(fileno(stdin), "rb"); if (file == NULL) error("can't gzdopen stdin"); gz_uncompress(file, stdout); } else { file = gzdopen(fileno(stdout), outmode); if (file == NULL) error("can't gzdopen stdout"); gz_compress(stdin, file); } } else { if (copyout) { SET_BINARY_MODE(stdout); } do { if (uncompr) { if (copyout) { file = gzopen(*argv, "rb"); if (file == NULL) fprintf(stderr, "%s: can't gzopen %s\n", prog, *argv); else gz_uncompress(file, stdout); } else { file_uncompress(*argv); } } else { if (copyout) { FILE * in = fopen(*argv, "rb"); if (in == NULL) { perror(*argv); } else { file = gzdopen(fileno(stdout), outmode); if (file == NULL) error("can't gzdopen stdout"); gz_compress(in, file); } } else { file_compress(*argv, outmode); } } } while (argv++, --argc); } return 0; }