|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void
zip (char *fnam)
{
char ch, *pt, str[256], st[10], _ch = 0, fname[20];
int i, size = 0, dist_ch, zipsize, chars[256] = { 0 };
int bitlen, plotter, _maxplot, bitpos = 0;
FILE *fw, *fr;
if ((fr = fopen (fnam, "r")) == 0)
{
printf ("Sorry input file \"%s\" cannot be opened\n", fnam);
exit (1);
}
while (!feof (fr))
//finding char density & file size
{
ch = fgetc (fr);
size++;
chars[(int) ch]++;
}
--size;
//original file size excluding EOF
rewind (fr);
//making file ready for reading again
for (i = 0, dist_ch = 0; i < 256; i++)
{
if (chars[i])
//listing chars present in file
{
str[dist_ch++] = i;
}
}
str[dist_ch] = '\0';
for (bitlen = 0, i = 1; i < dist_ch; i = i << 1, bitlen++);
strcpy (fname, fnam);
strcat (fname, ".zip");
//naming the output file
if ((fw = fopen (fname, "w")) == 0)
{
printf ("Sorry zip file cannot be written on disk\n");
exit (0);
}
sprintf (st, "%d ", size);
//calculating the size of zip file
zipsize = strlen (st);
sprintf (st, "%d ", strlen (str));
zipsize +=
(strlen (st) + strlen (str) + (int) (((bitlen * size) / 8.0) + 0.9));
for (_maxplot = 1, i = 1; i < bitlen; i++, _maxplot <<= 1);
//setting _maxplot
plotter = _maxplot;
sprintf (st, "%d ", _maxplot);
zipsize += strlen (st);
printf ("size of file: %dbytes\nsize of zip file: %dbytes\n", size, zipsize);
if (zipsize >= size)
{
printf ("This software is unable to zip this file\n");
exit (0);
}
//writing to zip file data to be used for
retrieving
fprintf (fw, "%d %d %d %s", size, strlen (str), _maxplot, str);
while (size--) //writting data in zipped format
{
ch = fgetc (fr);
pt = strchr (str, ch);
i = pt - str;
while (plotter)
{
if (plotter & i)
{
_ch <<= 1;
_ch |= 1;
}
else
_ch <<= 1;
plotter >>= 1;
if ((++bitpos) % 8 == 0)
{
fprintf (fw, "%c", _ch);
bitpos = 0;
}
}
plotter = _maxplot;
}
if (bitpos % 8 != 0)
{
while ((++bitpos) % 8 != 0)
_ch <<= 1;
_ch <<= 1;
fprintf (fw, "%c", _ch);
}
fclose (fw);
fclose (fr);
}
void
unzip (char *fnam)
{
char str[256], ch;
int size, _maxplot, strlen, i, plotter, bitpos, n;
FILE *fr;
if ((fr = fopen (fnam, "r")) == 0)
{
printf ("Sorry input file \"%s\" cannot be opened\n", fnam);
exit (0);
}
fscanf (fr, " %d %d %d", &size, &strlen, &_maxplot);
fgetc (fr);
for (i = 0; i < strlen; i++)
str[i] = fgetc (fr);
str[i] = '\0';
plotter = _maxplot >> 1;
i = 0;
bitpos = 0;
n = 0;
while (size >= 0)
{
ch = fgetc (fr);
for (i = 128; i; i >>= 1, plotter >>= 1)
{
if (i & ch)
{
n <<= 1;
n |= 1;
}
else
n <<= 1;
if (plotter == 0)
{
size--;
if (size >= 0)
printf ("%c", str[n]);
n = 0;
plotter = _maxplot;
}
}
}
}
int
main (int argc, char *argv[])
{
if (argc > 1)
{
if (!strcmp (argv[1], "-u"))
if (argc != 3)
{
if (argc == 2)
printf ("ERROR: missing filename\n");
else if (argc > 3)
printf ("ERROR: one filename allowed\n");
exit (1);
}
else
unzip (argv[2]);
else
{
if (argc > 2)
{
printf ("ERRROR: one filename allowed\n");
exit (1);
}
else
zip (argv[1]);
}
}
else
{
printf ("USAGE: zip [-u] <filename>\n");
exit (1);
}
return 0;
}
|