如何压缩输出文本文件?

时间:2015-06-08 22:09:48

标签: c++ c winapi

我的项目要求我通过生成的进程压缩输出文本文件,但我不知道如何做到这一点。这是我的源代码,用于在目录中搜索文件并将绝对文件路径写入文本文件。 //所有文件都在运行只是想知道如何压缩输出文件?

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<dir.h>
#include<string.h>
#define MAX 10000

//recursive search of directories
using namespace std;
class FileFinder
{
   int file_ctr;//How many files are there?
public:
    FileFinder(){file_ctr=0;}
    void getfile();
    void search(const char* path,const char* filename);
};

void FileFinder::getfile()
{
    char drive[10]={"G:\\"};//make it a 2D array and add all your Drives
    char filename[MAX];
    cout<<"Enter the File name\n";
    cin>>filename;
    FILE* fptr=fopen("test.txt","wt");//Delete the previous contents of File
    fclose(fptr);
    search(drive,filename);//loop through all drive[i]
    if(file_ctr==0)
        printf("\nNo File FOund!!!\n");
    else if(file_ctr==1)
    {
    char tmp[MAX];
    FILE* fptr=fopen("test.txt","rt");
    fgets(tmp,MAX,fptr);
    fclose(fptr);
    tmp[strlen(tmp)-1]=NULL;
    cout<<"\nFile found at Directory\t";
    printf("%s",tmp);
    }
    else
    {
       char tmp[MAX];
       FILE* fptr=fopen("test.txt","rt");
        cout<<"\n"<<file_ctr<<" Files Found!!\n";
       cout<<"\nFiles Found AT\n";
       for(int i=1;i<=file_ctr;i++)
       {
        fgets(tmp,MAX,fptr);
        tmp[strlen(tmp)-1]=NULL;
        cout<<"\n"<<i<<"\t"<<tmp;
        }
       fclose(fptr);
    }
    cout<<"\n\nPress any key to exit...";
    _getch( );
}
void FileFinder::search(const char* path,const char* filename)
{
    char tmp[MAX];
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind;
    strcpy(tmp,path);
    strcat(tmp,filename);
    hFind = FindFirstFile(tmp, &FindFileData);
    if(hFind != INVALID_HANDLE_VALUE)
    {
    while(hFind != INVALID_HANDLE_VALUE)
    {
        char tmp1[MAX];
        FILE* fptr=fopen("test.txt","at");
        strcpy(tmp1,path);
        cout<<"\nSearching...  "<<tmp1;
        strcat(tmp1,FindFileData.cFileName);
        fputs(tmp1,fptr);
        fputs("\n",fptr);
        fclose(fptr);
        file_ctr++;
        if (!FindNextFile(hFind, &FindFileData))
        {
            FindClose(hFind);
            hFind = INVALID_HANDLE_VALUE;
        }
    }
    }
    strcpy(tmp,path);
    strcat(tmp,"*.*");
    hFind = FindFirstFile(tmp, &FindFileData);
    while(hFind != INVALID_HANDLE_VALUE)
    {
       if(strcmp(FindFileData.cFileName,".")!=0 && strcmp(FindFileData.cFileName,"..")!=0 &&
                  FindFileData.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
       {
        char temp1[MAX];
        strcpy(temp1,path);
        strcat(temp1,FindFileData.cFileName);
        strcat(temp1,"\\");
        system("CLS");
        cout<<"\nSearching...  "<<temp1;
        search(temp1,filename);
       }
        if (!FindNextFile(hFind, &FindFileData))
        {
            FindClose(hFind);
            hFind = INVALID_HANDLE_VALUE;
        }
    }
}
int main()
{
    FileFinder f;
    f.getfile();
    getch();
    return 0;
}

0 个答案:

没有答案
相关问题