@@ -18,12 +18,8 @@ volatile int found = 0;
1818char found_password [PASSWORD_LENGTH + 1 ];
1919pthread_mutex_t lock ;
2020
21- int try_password (const char * zip_path , const char * password ) {
22- int err = 0 ;
23- zip_t * za = zip_open (zip_path , 0 , & err );
24- if (!za ) return 0 ;
21+ int try_password (zip_t * za , const char * password ) {
2522 if (zip_set_default_password (za , password ) < 0 ) {
26- zip_close (za );
2723 return 0 ;
2824 }
2925 zip_int64_t num_entries = zip_get_num_entries (za , 0 );
@@ -46,14 +42,19 @@ int try_password(const char* zip_path, const char* password) {
4642 break ;
4743 }
4844 }
49- zip_close (za );
5045 return success ;
5146}
5247
5348void * worker (void * arg ) {
5449 ThreadData * data = (ThreadData * )arg ;
5550 char password [PASSWORD_LENGTH + 1 ];
5651 password [PASSWORD_LENGTH ] = '\0' ;
52+ int err = 0 ;
53+ zip_t * za = zip_open (data -> zip_path , 0 , & err );
54+ if (!za ) {
55+ fprintf (stderr , "线程 %d 无法打开ZIP文件\n" , data -> thread_id );
56+ return NULL ;
57+ }
5758 for (int i = data -> start ; i <= data -> end ; i ++ ) {
5859 pthread_mutex_lock (& lock );
5960 if (found ) {
@@ -62,7 +63,7 @@ void* worker(void* arg) {
6263 }
6364 pthread_mutex_unlock (& lock );
6465 snprintf (password , PASSWORD_LENGTH + 1 , "%06d" , i );
65- if (try_password (data -> zip_path , password )) {
66+ if (try_password (za , password )) {
6667 pthread_mutex_lock (& lock );
6768 if (!found ) {
6869 found = 1 ;
@@ -72,6 +73,7 @@ void* worker(void* arg) {
7273 break ;
7374 }
7475 }
76+ zip_close (za );
7577 return NULL ;
7678}
7779
0 commit comments