commands/fzip/fzip.cpp
changeset 66 2a78c4ff2eab
parent 31 d0e1c40de386
child 78 b3ffff030d5c
equal deleted inserted replaced
63:6a2083f7eeb8 66:2a78c4ff2eab
    13 #include <zipfile.h>
    13 #include <zipfile.h>
    14 #include <ezgzip.h>
    14 #include <ezgzip.h>
    15 #include "fzip.h"
    15 #include "fzip.h"
    16 
    16 
    17 _LIT(KGzExtension, ".gz");
    17 _LIT(KGzExtension, ".gz");
       
    18 _LIT(KZipExtension, ".zip");
    18 
    19 
    19 CCommandBase* CCmdZip::NewLC()
    20 CCommandBase* CCmdZip::NewLC()
    20 	{
    21 	{
    21 	CCmdZip* self = new (ELeave) CCmdZip();
    22 	CCmdZip* self = new (ELeave) CCmdZip();
    22 	CleanupStack::PushL(self);
    23 	CleanupStack::PushL(self);
    24 	return self;
    25 	return self;
    25 	}
    26 	}
    26 
    27 
    27 CCmdZip::~CCmdZip()
    28 CCmdZip::~CCmdZip()
    28 	{
    29 	{
    29 	if (iFileToZip.Count() > 0)
    30 	iFileToZip.Close();
    30 		iFileToZip.Close();
       
    31 	}
    31 	}
    32 	
    32 	
    33 CCmdZip::CCmdZip() : CCommandBase(CCommandBase::EManualComplete)
    33 CCmdZip::CCmdZip() : CCommandBase(CCommandBase::EManualComplete)
    34 	{
    34 	{
    35 	}
    35 	}
    53 		if (iVerbose)
    53 		if (iVerbose)
    54 			{
    54 			{
    55 			// command-line sanity checks
    55 			// command-line sanity checks
    56 			if (iFileToZip.Count() > 0)
    56 			if (iFileToZip.Count() > 0)
    57 				{
    57 				{
    58 				PrintWarning(_L("Ignoring \'-f\' file option."));
    58 				PrintWarning(_L("--file option is not relevant when unzipping."));
    59 				}
    59 				}
    60 			if (iRecurse)
    60 			if (iRecurse)
    61 				{
    61 				{
    62 				PrintWarning(_L("Ignoring \'-r\' recurse option."));
    62 				PrintWarning(_L("--recurse option is not relevant when unzipping."));
    63 				}
    63 				}
    64 			}
    64 			}
    65 		ExpandArchiveL();
    65 		ExpandArchiveL();
    66 		}
    66 		}
    67 	else
    67 	else
    69 		if (iVerbose)
    69 		if (iVerbose)
    70 			{
    70 			{
    71 			// command-line sanity checks
    71 			// command-line sanity checks
    72 			if (iUnzipPath.Length() > 0)
    72 			if (iUnzipPath.Length() > 0)
    73 				{
    73 				{
    74 				PrintWarning(_L("Ignoring '-d' directory option."));	
    74 				PrintWarning(_L("--directory option is not relevant when zipping."));	
    75 				}
    75 				}
    76 			}
    76 			}
    77 		if (iFileToZip.Count() == 0)
    77 		if (iFileToZip.Count() == 0)
    78 			{
    78 			{
    79 			PrintError(KErrArgument, _L("Use '-f' to specify source files."));
    79 			LeaveIfErr(KErrArgument, _L("Specify some files to zip up using --file option."));
    80 			User::Leave(KErrArgument);
    80 			}
    81 			}
    81 		TRAPL(CreateArchiveL(), _L("Couldn't create %S"), &iArchive);
    82 		TRAPD(err, CreateArchiveL());
       
    83 		if (err != KErrNone)
       
    84 			{
       
    85 			PrintError(err, _L("Couldn't create archive"));
       
    86 			Fs().Delete(iArchive); // ignore error
       
    87 			User::Leave(err);
       
    88 			}
       
    89 		}
    82 		}
    90 	if (iVerbose)
    83 	if (iVerbose)
    91 		{
    84 		{
    92 		Printf(_L("Done\r\n"));	
    85 		Printf(_L("Done\r\n"));	
    93 		}
    86 		}
    94 	Complete(KErrNone);
    87 	Complete(KErrNone);
    95 	}
    88 	}
    96 	
    89 	
    97 void CCmdZip::ArgumentsL(RCommandArgumentList& aArguments)
    90 void CCmdZip::ArgumentsL(RCommandArgumentList& aArguments)
    98 	{
    91 	{
    99 	_LIT(KArg1, "archive");
    92 	_LIT(KArg1, "zipfile");
   100 	aArguments.AppendFileNameL(iArchive, KArg1);
    93 	aArguments.AppendFileNameL(iArchive, KArg1);
   101 	}
    94 	}
   102 
    95 
   103 void CCmdZip::OptionsL(RCommandOptionList& aOptions)
    96 void CCmdZip::OptionsL(RCommandOptionList& aOptions)
   104 	{
    97 	{
   117 	_LIT(KOptSource, "file");
   110 	_LIT(KOptSource, "file");
   118 	aOptions.AppendFileNameL(iFileToZip, KOptSource);
   111 	aOptions.AppendFileNameL(iFileToZip, KOptSource);
   119 
   112 
   120 	_LIT(KOptCompressionType, "compression-type");
   113 	_LIT(KOptCompressionType, "compression-type");
   121 	aOptions.AppendEnumL((TInt&)iCompressionType, KOptCompressionType);
   114 	aOptions.AppendEnumL((TInt&)iCompressionType, KOptCompressionType);
       
   115 
       
   116 	_LIT(KOptOverwrite, "overwrite");
       
   117 	aOptions.AppendBoolL(iOverwrite, KOptOverwrite);
   122 	}
   118 	}
   123 
   119 
   124 
   120 
   125 //
   121 //
   126 // COMPRESSION FUNCTIONS
   122 // COMPRESSION FUNCTIONS
   130 // CCmdZip::CreateArchiveL
   126 // CCmdZip::CreateArchiveL
   131 // determine which zip format to use and go ahead & create the archive
   127 // determine which zip format to use and go ahead & create the archive
   132 //
   128 //
   133 void CCmdZip::CreateArchiveL()
   129 void CCmdZip::CreateArchiveL()
   134 	{
   130 	{
       
   131 	if (iArchive.Length() == 0)
       
   132 		{
       
   133 		iArchive = iFileToZip[0];
       
   134 		iArchive.Append(iCompressionType == EGZip ? KGzExtension() : KZipExtension());
       
   135 		}
       
   136 
       
   137 	if (iArchive.Exists(FsL()))
       
   138 		{
       
   139 		if (iOverwrite)
       
   140 			{
       
   141 			FsL().Delete(iArchive);
       
   142 			}
       
   143 		else
       
   144 			{
       
   145 			LeaveIfErr(KErrAlreadyExists, _L("File %S already exists on disk. Use --overwrite or specify a different file"), &iArchive);
       
   146 			}
       
   147 		}
       
   148 
   135 	if (iCompressionType == EGZip)
   149 	if (iCompressionType == EGZip)
   136 		{
   150 		{
   137 		CreateGzArchiveL();
   151 		CreateGzArchiveL();
   138 		}
   152 		}
   139 	else
   153 	else
   156 	if (iFileToZip.Count() > 1)
   170 	if (iFileToZip.Count() > 1)
   157 		{
   171 		{
   158 		LeaveIfErr(KErrArgument, _L("GNU Zip format can only handle a single file"));
   172 		LeaveIfErr(KErrArgument, _L("GNU Zip format can only handle a single file"));
   159 		}
   173 		}
   160 
   174 
   161 	if (iArchive.Length() == 0)
   175 	if (iVerbose)
   162 		{
   176 		{
   163 		iArchive = iFileToZip[0];
   177 		Printf(_L("Creating '%S'\r\n"), &iArchive);
   164 		iArchive.Append(KGzExtension);
   178 		}
   165 		}
   179 
   166 
   180 	// open the input file
   167 	RFile input;
   181 	RFile input;
   168 	if (iVerbose)
   182 	User::LeaveIfError(input.Open(FsL(), iFileToZip[0], EFileStream | EFileRead | EFileShareAny));
   169 		{
       
   170 		Printf(_L("Creating '%S'\r\n"), &iArchive);
       
   171 		}
       
   172 
       
   173 	// open the input file
       
   174 	User::LeaveIfError(input.Open(Fs(), iFileToZip[0], EFileStream | EFileRead | EFileShareAny));
       
   175 	CleanupClosePushL(input);
   183 	CleanupClosePushL(input);
   176 
   184 
   177 	CEZFileToGZip* zip = CEZFileToGZip::NewLC(Fs(), iArchive, input);
   185 	CEZFileToGZip* zip = CEZFileToGZip::NewLC(FsL(), iArchive, input);
   178 	while (zip->DeflateL())
   186 	while (zip->DeflateL())
   179 		{
   187 		{
   180 		// do nothing
   188 		// do nothing
   181 		}
   189 		}
   182 	if (iVerbose)
   190 	if (iVerbose)
   295 	TInt err = Fs().MkDirAll(dest);
   303 	TInt err = Fs().MkDirAll(dest);
   296 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   304 	if ((err != KErrNone) && (err != KErrAlreadyExists))
   297 		{
   305 		{
   298 		LeaveIfErr(err, _L("Couldn't create path '%S'"), &dest);
   306 		LeaveIfErr(err, _L("Couldn't create path '%S'"), &dest);
   299 		}
   307 		}
   300 	User::LeaveIfError(newFile.Replace(Fs(), dest, EFileStream | EFileRead | EFileShareAny));
   308 	if (iOverwrite)
       
   309 		{
       
   310 		err = newFile.Replace(FsL(), dest, EFileStream | EFileRead | EFileWrite | EFileShareAny);
       
   311 		}
       
   312 	else
       
   313 		{
       
   314 		err = newFile.Create(FsL(), dest, EFileStream | EFileRead | EFileWrite | EFileShareAny);
       
   315 		}
       
   316 	LeaveIfErr(err, _L("Couldn't create file %S"), &dest);
   301 	CleanupClosePushL(newFile);
   317 	CleanupClosePushL(newFile);
   302 
   318 
   303 	// inflate the compressed file
   319 	// inflate the compressed file
   304 	CEZGZipToFile* zip = CEZGZipToFile::NewLC(Fs(), iArchive, newFile);
   320 	CEZGZipToFile* zip = CEZGZipToFile::NewLC(Fs(), iArchive, newFile);
   305 	while (zip->InflateL())
   321 	while (zip->InflateL())
   360 	// prep. the stream
   376 	// prep. the stream
   361 	RZipFileMemberReaderStream* readStream;
   377 	RZipFileMemberReaderStream* readStream;
   362 	aZip.GetInputStreamL(&aMember, readStream);
   378 	aZip.GetInputStreamL(&aMember, readStream);
   363 	CleanupStack::PushL(readStream);
   379 	CleanupStack::PushL(readStream);
   364 
   380 
   365 	LeaveIfErr(newFile.Replace(Fs(), dest, EFileShareExclusive), _L("Couldn't create file %S"), &dest);
   381 	if (iOverwrite)
       
   382 		{
       
   383 		err = newFile.Replace(Fs(), dest, EFileShareExclusive);
       
   384 		}
       
   385 	else
       
   386 		{
       
   387 		err = newFile.Create(Fs(), dest, EFileShareExclusive);
       
   388 		}
       
   389 	LeaveIfErr(err, _L("Couldn't create file %S"), &dest);
   366 	CleanupClosePushL(newFile);
   390 	CleanupClosePushL(newFile);
   367 	if (iVerbose)
   391 	if (iVerbose)
   368 		{
   392 		{
   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());
   393 		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());
   370 		}
   394 		}