locked
Working with zip files in C++ in Metro style app.

    Question

  • Hi!

    I need to unzip the zip archive with files in my app and then read that files. With this code i get file in the app dir:

    auto m_location = Windows::ApplicationModel::Package::Current->InstalledLocation;
    
    	auto view = m_location->GetFolderAsync(L"Resources\\");
    
    	view->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Storage::StorageFolder^>
    		(
    		[=](Windows::Foundation::IAsyncOperation<Windows::Storage::StorageFolder^>^ folder, Windows::Foundation::AsyncStatus status)
    	{
    		if (status == Windows::Foundation::AsyncStatus::Completed)
    		{
    			auto folder = view->GetResults();
    
    			auto fileOperation = folder->GetFileAsync("File.zip");
    			fileOperation->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Storage::StorageFile^>
    				(
    				[=](Windows::Foundation::IAsyncOperation<Windows::Storage::StorageFile^>^ operationResult, Windows::Foundation::AsyncStatus status)
    			{
    				if (status == Windows::Foundation::AsyncStatus::Completed)
    				{
    					auto readStream = file->OpenAsync(Windows::Storage::FileAccessMode::Read);
    
    	                                readStream->Completed = ref new Windows::Foundation::AsyncOperationCompletedHandler<Windows::Storage::Streams::IRandomAccessStream^>
    		(
    		[=](Windows::Foundation::IAsyncOperation<Windows::Storage::Streams::IRandomAccessStream^>^ operationResult, Windows::Foundation::AsyncStatus status)
    	        { 
    		     auto fileStream = operationResult->GetResults();
    
                        // I think here should start some unzipping code. 
    
                    }
    	        );
    				}
    				else
    				{
    					
    				}
    			}
    			);
    		}
    		else
    		{
    			
    		}
    	}
    	);
    I tried to use Windows::Storage::Compression::Decompressor with this stream
    auto fileStream = operationResult->GetResults();
    but after calling decompressor->ReadAsync i got empty buffer. But even if i got not empty buffer then how i should get files from decomressed data? So what i need to do to get streams for files which are stored in zip archive?


    Wednesday, May 30, 2012 2:21 PM

Answers

  • There isn't a class which understands Zip archives provided for C++ apps. Windows::Storage::Compression::Decompressor can decompress a zipped stream, but doesn't know anything about the format of the archive itself.

    There is a ZipArchive class in .Net which you can use in a Metro style app. You can wrap it in a Windows Runtime Component so you can call it from your C++ app. Other options would be to include a 3rd party zip library which.

    --Rob

    Wednesday, May 30, 2012 6:33 PM
    Owner