commands/fzip/fzip.cpp
changeset 31 d0e1c40de386
parent 0 7f656887cf89
child 66 2a78c4ff2eab
equal deleted inserted replaced
30:35cb3fe43f60 31:d0e1c40de386
    60 			if (iRecurse)
    60 			if (iRecurse)
    61 				{
    61 				{
    62 				PrintWarning(_L("Ignoring \'-r\' recurse option."));
    62 				PrintWarning(_L("Ignoring \'-r\' recurse option."));
    63 				}
    63 				}
    64 			}
    64 			}
    65 		TRAPL(ExpandArchiveL(), _L("Couldn't expand archive"));
    65 		ExpandArchiveL();
    66 		}
    66 		}
    67 	else
    67 	else
    68 		{
    68 		{
    69 		if (iVerbose)
    69 		if (iVerbose)
    70 			{
    70 			{
   321 	{
   321 	{
   322 	if (iVerbose)
   322 	if (iVerbose)
   323 		{
   323 		{
   324 		Printf(_L("Opening\t\t\'%S\'\r\n"), &iArchive);
   324 		Printf(_L("Opening\t\t\'%S\'\r\n"), &iArchive);
   325 		}
   325 		}
   326 	CZipFile* zip = CZipFile::NewL(Fs(), iArchive);
   326 	CZipFile* zip = NULL;
       
   327 	TRAPL(zip = CZipFile::NewL(Fs(), iArchive), _L("Couldn't create CZipFile for %S"), &iArchive);
   327 	CleanupStack::PushL(zip);
   328 	CleanupStack::PushL(zip);
   328 	CZipFileMemberIterator* zipIterator = zip->GetMembersL();
   329 	CZipFileMemberIterator* zipIterator = zip->GetMembersL();
   329 	CleanupStack::PushL(zipIterator);
   330 	CleanupStack::PushL(zipIterator);
   330 	CZipFileMember* zipMember = zipIterator->NextL();
   331 	CZipFileMember* zipMember = zipIterator->NextL();
   331 	while (zipMember)
   332 	while (zipMember)
   342 // CCmdZip::ExtractZipFileL
   343 // CCmdZip::ExtractZipFileL
   343 // extracts a single file from within the zip archive
   344 // extracts a single file from within the zip archive
   344 //
   345 //
   345 void CCmdZip::ExtractZipFileL(CZipFile& aZip, const CZipFileMember& aMember)
   346 void CCmdZip::ExtractZipFileL(CZipFile& aZip, const CZipFileMember& aMember)
   346 	{
   347 	{
   347 	// prep. the stream
       
   348 	RZipFileMemberReaderStream* readStream;
       
   349 	aZip.GetInputStreamL(&aMember, readStream);
       
   350 	CleanupStack::PushL(readStream);
       
   351 	// prep. the destination file. 
   348 	// prep. the destination file. 
   352 	// note if iUnzipPath is not specified, it'll stuff the extracted file in the current directory from which fzip.exe runs
   349 	// note if iUnzipPath is not specified, it'll stuff the extracted file in the current directory from which fzip.exe runs
   353 	RFile newFile;
   350 	RFile newFile;
   354 	TFileName2 dest(iUnzipPath);
   351 	TFileName2 dest(iUnzipPath);
   355 	dest.AppendComponentL(*aMember.Name());
   352 	dest.AppendComponentL(*aMember.Name());
   356 	TInt err = Fs().MkDirAll(dest);
   353 	TInt err = Fs().MkDirAll(dest);
   357 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   354 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   358 		{
   355 		{
   359 		User::Leave(err);
   356 		LeaveIfErr(err, _L("Couldn't create directory for file %S"), &dest);
   360 		}
   357 		}
   361 	User::LeaveIfError(newFile.Replace(Fs(), dest, EFileShareExclusive));
   358 	if (aMember.Name()->Right(1) == _L("\\")) return; // It's a directory entry, nothing more to be done
       
   359 
       
   360 	// prep. the stream
       
   361 	RZipFileMemberReaderStream* readStream;
       
   362 	aZip.GetInputStreamL(&aMember, readStream);
       
   363 	CleanupStack::PushL(readStream);
       
   364 
       
   365 	LeaveIfErr(newFile.Replace(Fs(), dest, EFileShareExclusive), _L("Couldn't create file %S"), &dest);
   362 	CleanupClosePushL(newFile);
   366 	CleanupClosePushL(newFile);
   363 	if (iVerbose)
   367 	if (iVerbose)
   364 		{
   368 		{
   365 		Printf(_L("Inflating '%S'\r\n\tcrc: 0x%x\r\n\tcompressed size: %d\r\n\tuncompressed size: %d\r\n"), &dest, aMember.CRC32(), aMember.CompressedSize(), aMember.UncompressedSize());
   369 		Printf(_L("Inflating '%S'\r\n\tcrc: 0x%x\r\n\tcompressed size: %d\r\n\tuncompressed size: %d\r\n"), &dest, aMember.CRC32(), aMember.CompressedSize(), aMember.UncompressedSize());
   366 		}
   370 		}
   374 		}
   378 		}
   375 	HBufC8* data = HBufC8::NewLC(length);
   379 	HBufC8* data = HBufC8::NewLC(length);
   376 	TPtr8 ptr = data->Des();
   380 	TPtr8 ptr = data->Des();
   377 	do
   381 	do
   378 		{
   382 		{
   379 		User::LeaveIfError(readStream->Read(ptr, length));
   383 		LeaveIfErr(readStream->Read(ptr, length), _L("Error reading from zip stream"));
   380 		User::LeaveIfError(newFile.Write(ptr));
   384 		LeaveIfErr(newFile.Write(ptr), _L("Error writing to file %S"), &dest);
   381 		bytesRead += length;
   385 		bytesRead += length;
   382 		if ((aMember.UncompressedSize() - bytesRead) < KDefaultZipBufferLength)
   386 		if ((aMember.UncompressedSize() - bytesRead) < KDefaultZipBufferLength)
   383 			{
   387 			{
   384 			length = aMember.UncompressedSize() - bytesRead;
   388 			length = aMember.UncompressedSize() - bytesRead;
   385 			}
   389 			}