📄 tmcompress.c
字号:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <zlib.h>
#include <sys/fcntlcom.h>
#include <unistd.h>
static const char *progname = NULL;
static unsigned long total_in = 0;
static unsigned long total_out = 0;
static const int True = 1;
static const int False = 0;
/*************************************************************************
Compression algorithm
**************************************************************************/
int do_compress(int fi, int fo)
{
uLong in_len;
uLongf out_len;
int lv_ret = 0;
void *inmem = NULL;
void *outmem = NULL;
struct stat st;
int total_size;
/* get size of input file */
if (fstat(fi,&st) == -1 )
{
printf("%s: fstat error with input file\n", progname);
close(fi);
exit(1);
}
/* save total size */
total_size = st.st_size;
/* allocate memory buffers */
inmem = malloc(total_size);
outmem = malloc(total_size + ((total_size*0.015)+1) +12 );
/* if memory allocation failed */
if (inmem == NULL || outmem == NULL)
{
printf("%s: out of memory\n", progname);
lv_ret = 1;
goto err;
}
/* fill in max output/input length */
in_len = total_size;
out_len = total_size + ((total_size*0.015)+1) +12;
/* write compressed block */
if ((lv_ret = read(fi,inmem,in_len)) != in_len)
{
printf("\nread error (disk error ?) %d %d\n",lv_ret, in_len);
goto err;
}
/* compress block */
lv_ret = compress(outmem,&out_len,inmem,in_len);
if (lv_ret != Z_OK)
{
/* this should NEVER happen */
printf("internal error - compression failed: %d, outlen %d, inlen %d\n",
lv_ret,out_len,in_len);
lv_ret = 2;
goto err;
}
/* write compressed block */
if ((lv_ret = write(fo,outmem,out_len)) != out_len)
{
printf("\nwrite error (disk full ?) %d %d\n",lv_ret, out_len);
goto err;
}
/* no error */
total_out = out_len;
total_in = in_len;
lv_ret = 0;
err:
free(outmem);
free(inmem);
return lv_ret;
}
/*************************************************************************
//
**************************************************************************/
int main(int argc, char *argv[])
{
int i = 1;
int ret = 0;
int fi = 0;
int fo = 0;
const char *in_name = NULL;
const char *out_name = NULL;
progname = argv[0];
/* if three arguements were not input */
if (3 != argc)
{
printf("usage: %s input-file output-file\n", progname);
exit(1);
}
/* get input file name */
in_name = argv[i++];
/* open the input files */
fi = open(in_name,O_RDONLY);
if (fi == -1)
{
printf("%s: cannot open input file %s\n", progname, in_name);
exit(1);
}
/* get output file name */
out_name = argv[i++];
/* open/create output file */
fo = open(out_name,O_WRONLY|O_CREAT);
if (fo == -1)
{
printf("%s: cannot create output file %s\n", progname, out_name);
close(fi);
exit(1);
}
/* compress file */
ret = do_compress(fi,fo);
if (ret == 0)
printf("%14s, compression %2.2f, before %6ld bytes, after %6ld bytes\n",in_name,(total_out*100.0)/total_in, total_in, total_out);
/* close all files */
close(fi);
if (fo) close(fo);
/* make the output file read/writeable/executable */
chmod(out_name,0x1FF);
/* exit */
return ret;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -