sl@0: // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "eustd.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: sl@0: void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname); sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-EZLIB-CT-0829 sl@0: @SYMTestCaseDesc Gzip functionality test sl@0: @SYMTestPriority High sl@0: @SYMTestActions Decompress and compress a zip file read from the command line. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: sl@0: LOCAL_C void doExampleL() sl@0: { sl@0: RFs rfs; sl@0: rfs.Connect(); sl@0: TBool compress = ETrue; sl@0: TInt bufferSize = 0x8000; sl@0: TBool readHeader = EFalse; sl@0: TBool readTrailer = EFalse; sl@0: TBool noWork = EFalse; sl@0: sl@0: TInt cmdLineLen = User::CommandLineLength(); sl@0: sl@0: if (cmdLineLen <= 0) sl@0: { sl@0: _LIT(KUsage,"Usage:gzip [-dht] [-u bufferSize] filename\n"); sl@0: console->Printf(KUsage); sl@0: User::Leave(1); sl@0: } sl@0: //(cmdLineLen > 0) case sl@0: HBufC *argv = HBufC::NewLC(cmdLineLen); sl@0: TPtr cmd(argv->Des()); sl@0: User::CommandLine(cmd); sl@0: sl@0: TLex arguments(*argv); sl@0: sl@0: TPtrC options(arguments.NextToken()); sl@0: TBool expectBufferSize = EFalse; sl@0: _LIT(KBadBufferSize,"Bad buffersize specified\n"); sl@0: _LIT(KUnknownOption,"Unknown Options %S\n"); sl@0: sl@0: sl@0: while (options[0]=='-' || expectBufferSize) sl@0: { sl@0: TInt i = 1; sl@0: sl@0: if (expectBufferSize) sl@0: { sl@0: expectBufferSize = EFalse; sl@0: if (options.Length() == 0) sl@0: { sl@0: console->Printf(KBadBufferSize); sl@0: User::Leave(1); sl@0: } sl@0: else sl@0: { sl@0: TLex bufLex(options); sl@0: if (bufLex.Val(bufferSize) != KErrNone) sl@0: { sl@0: console->Printf(KBadBufferSize); sl@0: User::Leave(1); sl@0: } sl@0: } sl@0: } sl@0: else sl@0: { sl@0: sl@0: while (i < options.Length()) sl@0: { sl@0: if (options[i] == 'd') sl@0: compress = EFalse; sl@0: else if (options[i] == 'b') sl@0: { sl@0: if (i + 1 < options.Length()) sl@0: { sl@0: TLex bufLex(options.Right(options.Length() - (i + 1))); sl@0: if (bufLex.Val(bufferSize) != KErrNone) sl@0: { sl@0: console->Printf(KBadBufferSize); sl@0: User::Leave(1); sl@0: } sl@0: } sl@0: else sl@0: expectBufferSize = ETrue; sl@0: } sl@0: else if (options[i] == 'h') sl@0: readHeader = noWork = ETrue; sl@0: else if (options[i] == 't') sl@0: readTrailer = noWork = ETrue; sl@0: else sl@0: { sl@0: console->Printf(KUnknownOption,&options); sl@0: i = options.Length(); sl@0: } sl@0: i++; sl@0: } sl@0: sl@0: if (i == 1) sl@0: { sl@0: _LIT(KNoOption,"No option specified\n"); sl@0: console->Printf(KNoOption); sl@0: User::Leave(1); sl@0: } sl@0: } sl@0: options.Set(arguments.NextToken()); sl@0: } sl@0: sl@0: console->Printf(_L("Buffer Size %d\n"),bufferSize); sl@0: sl@0: if (readHeader) sl@0: { sl@0: ReadAndPrintHeaderL(rfs,options); sl@0: } sl@0: sl@0: if (readTrailer) sl@0: { sl@0: TEZGZipTrailer trailer; sl@0: EZGZipFile::LocateAndReadTrailerL(rfs, options, trailer); sl@0: _LIT(KTrailer,"Crc = %d Size = %d\n"); sl@0: console->Printf(KTrailer,trailer.iCrc32,trailer.iSize); sl@0: } sl@0: sl@0: if (!noWork) sl@0: { sl@0: if (!compress) sl@0: { sl@0: sl@0: TPtrC inputFile(options); sl@0: sl@0: HBufC *uncompressedFile = HBufC::NewLC(inputFile.Length()+1); sl@0: _LIT(KUfl,"%S1"); sl@0: uncompressedFile->Des().Format(KUfl,&inputFile); sl@0: sl@0: RFile output; sl@0: TInt err; sl@0: sl@0: _LIT(KInfo,"Decompressing file %S\n"); sl@0: console->Printf(KInfo,&inputFile); sl@0: sl@0: err = output.Create(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive); sl@0: if (err == KErrAlreadyExists) sl@0: User::LeaveIfError(output.Open(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive)); sl@0: else sl@0: User::LeaveIfError(err); sl@0: CleanupClosePushL(output); sl@0: sl@0: CEZGZipToFile *def = CEZGZipToFile::NewLC(rfs,inputFile,output,bufferSize); sl@0: while (def->InflateL()){/*do nothing*/} sl@0: sl@0: _LIT(KHoorah,"Hoorah"); sl@0: console->Printf(KHoorah); sl@0: sl@0: CleanupStack::PopAndDestroy(3); sl@0: } sl@0: else sl@0: { sl@0: TPtrC inputFile(options); sl@0: sl@0: HBufC *compressedFile = HBufC::NewLC(inputFile.Length()+3); sl@0: _LIT(KUfl,"%S.gz"); sl@0: compressedFile->Des().Format(KUfl,&inputFile); sl@0: sl@0: RFile input; sl@0: sl@0: _LIT(KInfo,"Compressing file %S to %S\n"); sl@0: console->Printf(KInfo,&inputFile,compressedFile); sl@0: sl@0: User::LeaveIfError(input.Open(rfs,inputFile,EFileStream | EFileRead | EFileShareAny)); sl@0: CleanupClosePushL(input); sl@0: sl@0: CEZFileToGZip *com = CEZFileToGZip::NewLC(rfs,*compressedFile,input,bufferSize); sl@0: while (com->DeflateL()){/*do nothing*/} sl@0: sl@0: _LIT(KHoorah,"Hoorah"); sl@0: console->Printf(KHoorah); sl@0: sl@0: CleanupStack::PopAndDestroy(3); sl@0: } sl@0: } sl@0: CleanupStack::PopAndDestroy(1); sl@0: rfs.Close(); sl@0: } sl@0: sl@0: sl@0: void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname) sl@0: { sl@0: TEZGZipHeader header; sl@0: sl@0: if (!EZGZipFile::IsGzipFileL(rfs,fname)) sl@0: { sl@0: _LIT(KNotGzipFile,"%S is not a gzip file\n"); sl@0: console->Printf(KNotGzipFile,&fname); sl@0: User::Leave(1); sl@0: } sl@0: RFile gzipFile; sl@0: User::LeaveIfError(gzipFile.Open(rfs,fname,EFileStream | EFileRead | EFileShareAny)); sl@0: EZGZipFile::ReadHeaderL(gzipFile,header); sl@0: sl@0: _LIT(KFileIds,"ID1 = %d ID2 = %d\n"); sl@0: console->Printf(KFileIds,header.iId1,header.iId2); sl@0: _LIT(KCompressionMethod,"Compression Method = %d\n"); sl@0: console->Printf(KCompressionMethod,header.iCompressionMethod); sl@0: _LIT(KFlags,"Flags = %d\n"); sl@0: console->Printf(KFlags,header.iFlags); sl@0: _LIT(KTime,"Time Stamp = %d\n"); sl@0: console->Printf(KTime,header.iTime); sl@0: _LIT(KExtraFlags,"Extra Flags %d\n"); sl@0: console->Printf(KExtraFlags,header.iExtraFlags); sl@0: _LIT(KOS,"OS %d\n"); sl@0: console->Printf(KOS,header.iOs); sl@0: if (header.iFlags & 4) sl@0: { sl@0: _LIT(KExtraLen,"Extra Length %d\n"); sl@0: console->Printf(KExtraLen,header.iXlen); sl@0: HBufC *buf = HBufC::NewMaxLC(header.iExtra->Length()); sl@0: buf->Des().Copy(*header.iExtra); sl@0: console->Printf(*buf); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: if (header.iFlags & 8) sl@0: { sl@0: _LIT(KName,"Name: %S\n"); sl@0: HBufC *buf = HBufC::NewMaxLC(header.iFname->Length()); sl@0: buf->Des().Copy(*header.iFname); sl@0: console->Printf(KName,buf); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: if (header.iFlags & 16) sl@0: { sl@0: _LIT(KComment,"Comment: %S\n"); sl@0: HBufC *buf = HBufC::NewMaxLC(header.iComment->Length()); sl@0: buf->Des().Copy(*header.iComment); sl@0: console->Printf(KComment,buf); sl@0: CleanupStack::PopAndDestroy(); sl@0: } sl@0: sl@0: if (header.iFlags & 2) sl@0: { sl@0: _LIT(KCrc,"Crc16 = %d\n"); sl@0: console->Printf(KCrc,header.iCrc); sl@0: } sl@0: }