winapi - How to remove .zip file in c on windows? (error: Directory not empty) -
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include "win32-dirent.h" #include <windows.h> #include <io.h> #include <direct.h> #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 <target folder name>\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; }
output is:
-------------------------- archive: 1234.zip inflating: 1234/4.txt inflating: 1234/56/5.txt inflating: 1234/56/6.txt inflating: 1234/1_23.zip --------------------------
your code looks feasible @ first glance, recursively clean directories before attempting remove them.
one thing comes mind, possibility have zip file opened in application, hence possibly locked.
you won't "directory not empty" errors unless, well, directory isn't empty.
for start, change line:
if(_rmdir(foldername) != 0)perror(foldername);
to:
if(_rmdir(foldername) != 0) { char buf[1000]; sprintf(buf,"dir \"%s\"",foldername); system(buf); perror(foldername); }
and should tell directory you're in , offending files are.
Comments
Post a Comment