如何在C中解压缩文件

时间:2014-11-30 16:03:08

标签: c compression

我有用C编写的代码,我想解压缩但我无法这样做。

请在这方面帮助我。我想解压缩文件夹中的文本文件 。我在减压方法中迷失了,不知道怎么做

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char find[100000];

char* StrCompress(char myStr[]);

int go()
{
    FILE* custom = NULL;
    custom = fopen("newfile.txt", "r");
    if(custom == NULL)
    {
        printf("\n Unable to open file testing.txt");
        return 1;
    }

    FILE* future = NULL;
    future = fopen("new.txt", "w");
    if(future== NULL)
    {
        printf("\n Unable to open file new.txt");
        return 1;
    }

     while(fscanf(custom, "%s", find)!=EOF)
     {
     //Use data read in find here
        printf("%s", StrCompress(find));
        fprintf(future, "%s ", StrCompress(find));
     }

fclose(custom);
fclose(future);
}

char* StrCompress(char myStr[])
{
    char *s = myStr;
    char *r, *p;
    int count, i;

    while (*s)
    {
        count = 1;

        while (*s == *(s+1) && *s)
        {
            count++;
            s++;
        }

        if (count > 1)
        {
            *(s - count + 2) = count + '0';

            for (i = 0; i < count - 2; i++)
            {
                p = s + 1;
                r = s;

                while (*r)
                    *r++ = *p++;
                                    s--;
            }
        }
        s++;
    }

    return myStr;
}

int main()
{
    go();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

来自维基百科: WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW 如果我们将行程编码(RLE)数据压缩算法应用于上述假设扫描线,我们得到以下结果: 12W1B12W3B24W1B14W

因此,如果你的压缩是正确的(没有检查但你的问题是关于解压缩),那么取回原始字符串非常简单。

  • 通过char
  • 读取流char
  • 获取int,这是char将被使用的时间
  • 获取相应的字符
  • 打印
  • 继续
相关问题