0

サーバーから .zip ファイルをダウンロードし、解凍後にプロジェクトに使用する必要があります。.sqlite ファイルのダウンロードに成功し、右クリック (手動) を使用して展開できますが、minizip と SSZipArchive または ZipArchive を使用して展開しようとすると、「ファイルを開けません」というエラーが表示されます。

.sqlite を手動で抽出し、Mac で右クリックして .sqlite ファイルを圧縮すると、コードは完全に抽出されますが、ダウンロードしたファイルを直接抽出しようとすると、この問題が発生します。

ファイルの書き込みと抽出に以下の名前形式を適用しました。

  1. ファイルの書き込み中、名前は XYZ.sqlite.zip でしたが、エラーも発生しています
  2. XYZ.zip および含まれるファイル名は XYZ.sqlite です。これもエラーを与えます
  3. また、XYZ.sqlite.zip という名前のファイルを作成し、その名前を XYZ.zip に変更してから抽出しましたが、これも機能しませんでした。

以下はコードです。

ファイルをダウンロードする場合:

 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:zipURL]];
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:[zipURL lastPathComponent]];
operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
 [operation.securityPolicy setAllowInvalidCertificates:YES];
 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
 [self unzipDBFile:path];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[operation setProgressiveDownloadProgressBlock:
 ^(AFDownloadRequestOperation *operation, NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {

     NSArray *progress = [NSArray arrayWithObjects:obj_Pro,[NSNumber numberWithFloat:totalBytesReadForFile/(float)totalBytesExpectedToReadForFile], nil];

     [self performSelectorOnMainThread:@selector(updateProgressBar:) withObject:progress waitUntilDone:NO];
 }];
[operation start];

ファイルを解凍する場合:

-(void)unzipDBFile:(NSString *)filePath
{

 NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES) objectAtIndex:0];
   NSString *outputPath = [documentDir stringByAppendingPathComponent:@"/DBFolder"];
   BOOL isExtracted =[SSZipArchive unzipFileAtPath:outputPath toDestination:documentDir];

  // NSString *filepath = [[NSBundle mainBundle] pathForResource:@"ZipFileName" ofType:@"zip"];
 //    ZipArchive *zipArchive = [[ZipArchive alloc] init];
 //    [zipArchive UnzipOpenFile:filePath];

 //    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *path = [paths objectAtIndex:0];
//    BOOL isExtracted=[zipArchive UnzipFileTo:path overWrite:YES];
//    NSLog(@"PATH=%@",path);
   if (!isExtracted)
    {
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error in extracting" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
      [alert show];
      [alert release];
   }else{
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Extract Success" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
     [alert show];
     [alert release];
   }
//    [zipArchive UnzipCloseFile];
//    [zipArchive release];
}

以下は、プログレスバーを更新するためのコードです。

- (void)updateProgressBar:(NSArray *)progress
{
 UIProgressView *pgr = (UIProgressView *)[progress objectAtIndex:0];
 [pgr setProgress:[[progress objectAtIndex:1] floatValue]];
}
4

1 に答える 1