How to remove .zip file in c on windows? (error: Directory not empty)

Posted by ExtremeBlue on Stack Overflow See other posts from Stack Overflow or by ExtremeBlue
Published on 2011-02-09T07:19:04Z Indexed on 2011/02/09 7:25 UTC
Read the original article Hit count: 135

Filed under:
|
|
|
|

include

include

include

include "win32-dirent.h"

include

include

include

define MAXFILEPATH 1024

bool IsDirectory(char* path) { WIN32_FIND_DATA w32fd; HANDLE hFindFile;

hFindFile = FindFirstFile((PTCHAR)path, &w32fd);

if(hFindFile == INVALID_HANDLE_VALUE)
{
    return false;
}
return w32fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY); 

}

int RD(const char* folderName) { DIR *dir; struct dirent *ent;

    dir = opendir(folderName);
    if(dir != NULL)
    {
            while((ent = readdir(dir)) != NULL)
            {
                    if(strcmp(ent->d_name , ".") == 0 ||
                            strcmp(ent->d_name, "..") == 0)
                    {
                            continue;
                    }

                    char fileName[MAXFILEPATH];
                    sprintf(fileName,"%s%c%s", folderName, '\\', ent->d_name);                        
                    if(IsDirectory(fileName))
                    {
                        RD(fileName);
                    }
                    else
                    {
                        unlink(fileName);
                    }
            }

            closedir(dir);

            //chmod(folderName, S_IWRITE | S_IREAD);
            if(_rmdir(folderName) != 0)perror(folderName);              

    }
    else
    {
            printf("%s <%s>\n","Could Not Open Directory.", folderName);
            return -1;
    }

    return 0;

}

int main(int argc, char* argv[]) { if(argc < 2) { printf("usage: ./a.out \n"); return 1; }

    //RD(argv[1]);
    //_mkdir("12");
    //_mkdir("12\\34");
    //_rmdir("12\\34");
    //_rmdir("12");
    char buf[0xff];
    sprintf(buf, "unzip -x -q -d 1234 1234.zip");
    system(buf);

    RD("1234");

    //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\5.txt");
    //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56\\6.txt");
    //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\1_23.zip");
    //unlink("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\4.txt");
    //_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234\\56");
    //_rmdir("D:\\dev\\c\\project\\removeFolder\\Debug\\1234");

    return 0;

}


Archive: 1234.zip inflating: 1234/4.txt inflating: 1234/56/5.txt inflating: 1234/56/6.txt

inflating: 1234/1_23.zip

© Stack Overflow or respective owner

Related posts about c

    Related posts about Windows