1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/compressionlibs/ziplib/test/rtest/gzip/gzip.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,267 @@
1.4 +// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include "eustd.h"
1.20 +#include <ezcompressor.h>
1.21 +#include <ezdecompressor.h>
1.22 +#include <ezlib.h>
1.23 +#include <ezgzip.h>
1.24 +
1.25 +#include <f32file.h>
1.26 +
1.27 +void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname);
1.28 +
1.29 +/**
1.30 +@SYMTestCaseID SYSLIB-EZLIB-CT-0829
1.31 +@SYMTestCaseDesc Gzip functionality test
1.32 +@SYMTestPriority High
1.33 +@SYMTestActions Decompress and compress a zip file read from the command line.
1.34 +@SYMTestExpectedResults Test must not fail
1.35 +@SYMREQ REQ0000
1.36 +*/
1.37 +
1.38 +LOCAL_C void doExampleL()
1.39 + {
1.40 + RFs rfs;
1.41 + rfs.Connect();
1.42 + TBool compress = ETrue;
1.43 + TInt bufferSize = 0x8000;
1.44 + TBool readHeader = EFalse;
1.45 + TBool readTrailer = EFalse;
1.46 + TBool noWork = EFalse;
1.47 +
1.48 + TInt cmdLineLen = User::CommandLineLength();
1.49 +
1.50 + if (cmdLineLen <= 0)
1.51 + {
1.52 + _LIT(KUsage,"Usage:gzip [-dht] [-u bufferSize] filename\n");
1.53 + console->Printf(KUsage);
1.54 + User::Leave(1);
1.55 + }
1.56 + //(cmdLineLen > 0) case
1.57 + HBufC *argv = HBufC::NewLC(cmdLineLen);
1.58 + TPtr cmd(argv->Des());
1.59 + User::CommandLine(cmd);
1.60 +
1.61 + TLex arguments(*argv);
1.62 +
1.63 + TPtrC options(arguments.NextToken());
1.64 + TBool expectBufferSize = EFalse;
1.65 + _LIT(KBadBufferSize,"Bad buffersize specified\n");
1.66 + _LIT(KUnknownOption,"Unknown Options %S\n");
1.67 +
1.68 +
1.69 + while (options[0]=='-' || expectBufferSize)
1.70 + {
1.71 + TInt i = 1;
1.72 +
1.73 + if (expectBufferSize)
1.74 + {
1.75 + expectBufferSize = EFalse;
1.76 + if (options.Length() == 0)
1.77 + {
1.78 + console->Printf(KBadBufferSize);
1.79 + User::Leave(1);
1.80 + }
1.81 + else
1.82 + {
1.83 + TLex bufLex(options);
1.84 + if (bufLex.Val(bufferSize) != KErrNone)
1.85 + {
1.86 + console->Printf(KBadBufferSize);
1.87 + User::Leave(1);
1.88 + }
1.89 + }
1.90 + }
1.91 + else
1.92 + {
1.93 +
1.94 + while (i < options.Length())
1.95 + {
1.96 + if (options[i] == 'd')
1.97 + compress = EFalse;
1.98 + else if (options[i] == 'b')
1.99 + {
1.100 + if (i + 1 < options.Length())
1.101 + {
1.102 + TLex bufLex(options.Right(options.Length() - (i + 1)));
1.103 + if (bufLex.Val(bufferSize) != KErrNone)
1.104 + {
1.105 + console->Printf(KBadBufferSize);
1.106 + User::Leave(1);
1.107 + }
1.108 + }
1.109 + else
1.110 + expectBufferSize = ETrue;
1.111 + }
1.112 + else if (options[i] == 'h')
1.113 + readHeader = noWork = ETrue;
1.114 + else if (options[i] == 't')
1.115 + readTrailer = noWork = ETrue;
1.116 + else
1.117 + {
1.118 + console->Printf(KUnknownOption,&options);
1.119 + i = options.Length();
1.120 + }
1.121 + i++;
1.122 + }
1.123 +
1.124 + if (i == 1)
1.125 + {
1.126 + _LIT(KNoOption,"No option specified\n");
1.127 + console->Printf(KNoOption);
1.128 + User::Leave(1);
1.129 + }
1.130 + }
1.131 + options.Set(arguments.NextToken());
1.132 + }
1.133 +
1.134 + console->Printf(_L("Buffer Size %d\n"),bufferSize);
1.135 +
1.136 + if (readHeader)
1.137 + {
1.138 + ReadAndPrintHeaderL(rfs,options);
1.139 + }
1.140 +
1.141 + if (readTrailer)
1.142 + {
1.143 + TEZGZipTrailer trailer;
1.144 + EZGZipFile::LocateAndReadTrailerL(rfs, options, trailer);
1.145 + _LIT(KTrailer,"Crc = %d Size = %d\n");
1.146 + console->Printf(KTrailer,trailer.iCrc32,trailer.iSize);
1.147 + }
1.148 +
1.149 + if (!noWork)
1.150 + {
1.151 + if (!compress)
1.152 + {
1.153 +
1.154 + TPtrC inputFile(options);
1.155 +
1.156 + HBufC *uncompressedFile = HBufC::NewLC(inputFile.Length()+1);
1.157 + _LIT(KUfl,"%S1");
1.158 + uncompressedFile->Des().Format(KUfl,&inputFile);
1.159 +
1.160 + RFile output;
1.161 + TInt err;
1.162 +
1.163 + _LIT(KInfo,"Decompressing file %S\n");
1.164 + console->Printf(KInfo,&inputFile);
1.165 +
1.166 + err = output.Create(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive);
1.167 + if (err == KErrAlreadyExists)
1.168 + User::LeaveIfError(output.Open(rfs, *uncompressedFile,EFileStream | EFileWrite | EFileShareExclusive));
1.169 + else
1.170 + User::LeaveIfError(err);
1.171 + CleanupClosePushL(output);
1.172 +
1.173 + CEZGZipToFile *def = CEZGZipToFile::NewLC(rfs,inputFile,output,bufferSize);
1.174 + while (def->InflateL()){/*do nothing*/}
1.175 +
1.176 + _LIT(KHoorah,"Hoorah");
1.177 + console->Printf(KHoorah);
1.178 +
1.179 + CleanupStack::PopAndDestroy(3);
1.180 + }
1.181 + else
1.182 + {
1.183 + TPtrC inputFile(options);
1.184 +
1.185 + HBufC *compressedFile = HBufC::NewLC(inputFile.Length()+3);
1.186 + _LIT(KUfl,"%S.gz");
1.187 + compressedFile->Des().Format(KUfl,&inputFile);
1.188 +
1.189 + RFile input;
1.190 +
1.191 + _LIT(KInfo,"Compressing file %S to %S\n");
1.192 + console->Printf(KInfo,&inputFile,compressedFile);
1.193 +
1.194 + User::LeaveIfError(input.Open(rfs,inputFile,EFileStream | EFileRead | EFileShareAny));
1.195 + CleanupClosePushL(input);
1.196 +
1.197 + CEZFileToGZip *com = CEZFileToGZip::NewLC(rfs,*compressedFile,input,bufferSize);
1.198 + while (com->DeflateL()){/*do nothing*/}
1.199 +
1.200 + _LIT(KHoorah,"Hoorah");
1.201 + console->Printf(KHoorah);
1.202 +
1.203 + CleanupStack::PopAndDestroy(3);
1.204 + }
1.205 + }
1.206 + CleanupStack::PopAndDestroy(1);
1.207 + rfs.Close();
1.208 + }
1.209 +
1.210 +
1.211 +void ReadAndPrintHeaderL(RFs &rfs, const TDesC &fname)
1.212 + {
1.213 + TEZGZipHeader header;
1.214 +
1.215 + if (!EZGZipFile::IsGzipFileL(rfs,fname))
1.216 + {
1.217 + _LIT(KNotGzipFile,"%S is not a gzip file\n");
1.218 + console->Printf(KNotGzipFile,&fname);
1.219 + User::Leave(1);
1.220 + }
1.221 + RFile gzipFile;
1.222 + User::LeaveIfError(gzipFile.Open(rfs,fname,EFileStream | EFileRead | EFileShareAny));
1.223 + EZGZipFile::ReadHeaderL(gzipFile,header);
1.224 +
1.225 + _LIT(KFileIds,"ID1 = %d ID2 = %d\n");
1.226 + console->Printf(KFileIds,header.iId1,header.iId2);
1.227 + _LIT(KCompressionMethod,"Compression Method = %d\n");
1.228 + console->Printf(KCompressionMethod,header.iCompressionMethod);
1.229 + _LIT(KFlags,"Flags = %d\n");
1.230 + console->Printf(KFlags,header.iFlags);
1.231 + _LIT(KTime,"Time Stamp = %d\n");
1.232 + console->Printf(KTime,header.iTime);
1.233 + _LIT(KExtraFlags,"Extra Flags %d\n");
1.234 + console->Printf(KExtraFlags,header.iExtraFlags);
1.235 + _LIT(KOS,"OS %d\n");
1.236 + console->Printf(KOS,header.iOs);
1.237 + if (header.iFlags & 4)
1.238 + {
1.239 + _LIT(KExtraLen,"Extra Length %d\n");
1.240 + console->Printf(KExtraLen,header.iXlen);
1.241 + HBufC *buf = HBufC::NewMaxLC(header.iExtra->Length());
1.242 + buf->Des().Copy(*header.iExtra);
1.243 + console->Printf(*buf);
1.244 + CleanupStack::PopAndDestroy();
1.245 + }
1.246 +
1.247 + if (header.iFlags & 8)
1.248 + {
1.249 + _LIT(KName,"Name: %S\n");
1.250 + HBufC *buf = HBufC::NewMaxLC(header.iFname->Length());
1.251 + buf->Des().Copy(*header.iFname);
1.252 + console->Printf(KName,buf);
1.253 + CleanupStack::PopAndDestroy();
1.254 + }
1.255 +
1.256 + if (header.iFlags & 16)
1.257 + {
1.258 + _LIT(KComment,"Comment: %S\n");
1.259 + HBufC *buf = HBufC::NewMaxLC(header.iComment->Length());
1.260 + buf->Des().Copy(*header.iComment);
1.261 + console->Printf(KComment,buf);
1.262 + CleanupStack::PopAndDestroy();
1.263 + }
1.264 +
1.265 + if (header.iFlags & 2)
1.266 + {
1.267 + _LIT(KCrc,"Crc16 = %d\n");
1.268 + console->Printf(KCrc,header.iCrc);
1.269 + }
1.270 + }