gbk数据解压缩转成Unicode编码数据

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const TBool GBKCompressToUnicodeL(const TDesC8& aGBKData,RBuf& aUnicodeData)
{
	z_stream stream;   
	memset(&stream, 0, sizeof(z_stream));   

	const uInt inputBufLen = aGBKData.Length() + 1;

	stream.avail_in = inputBufLen;
	stream.next_in = (Bytef*)aGBKData.Ptr(); 

	TInt res = inflateInit(&stream);

	TBool isUncompressOK = EFalse ;

	RBuf8 gbkUncompressData;
	CleanupClosePushL(gbkUncompressData);
	gbkUncompressData.Create((inputBufLen<<3));
	gbkUncompressData.Zero();
	
	if ( res == Z_OK)
	{
		TInt pakLen = 256;
		Bytef* pakBuf = NULL;
		
		if ( inputBufLen > 120 * 1024 )
		{
			pakLen = 4096;
		}
		else if ( inputBufLen > 80 * 1024 )
		{
			pakLen = 2048;
		}
		else if ( inputBufLen > 50 * 1024 )
		{
			pakLen = 1024;
		}
		else if ( inputBufLen > 20 * 1024 )
		{
			pakLen = 512;
		}
		
		pakBuf = new (ELeave)Bytef[pakLen];

		TInt memAllocRes = KErrNone;
		
		while(stream.avail_out == 0)
		{
			memset(pakBuf,0,pakLen * sizeof(Bytef));

			stream.avail_out = pakLen;
			stream.next_out = pakBuf; 

			res = inflate(&stream, Z_NO_FLUSH);
			
			switch (res)
			{
			case Z_NEED_DICT:
			case Z_DATA_ERROR:
			case Z_MEM_ERROR:
			case Z_STREAM_ERROR:
			case Z_BUF_ERROR:
			{
				isUncompressOK = EFalse;
				break;
			}

			default:
			{
				if ( res == Z_OK ||  res == Z_STREAM_END)
				{
					const TInt  dataLen = pakLen - stream.avail_out;
					
					const TInt curBufLen = gbkUncompressData.Length();
					const TInt maxBufLen = gbkUncompressData.MaxLength();

					if ( curBufLen + dataLen > maxBufLen)
					{
						memAllocRes = gbkUncompressData.ReAlloc(curBufLen + dataLen + (pakLen<<3));
					}

					if ( KErrNone == memAllocRes )
					{
						gbkUncompressData.Append((const TUint8*)pakBuf,dataLen);
						isUncompressOK = ETrue;
					}
				}

				break;
			}

			}
			
			if ( KErrNone != memAllocRes )
			{
				isUncompressOK = EFalse;
				break;
			}
		}
		
		
		delete[] pakBuf;
		pakBuf = NULL;
	}

	const TInt len = stream.total_out;

	inflateEnd(&stream);

	aUnicodeData.Close();

	if ( isUncompressOK )
	{
		RFs& fs = CEikonEnv::Static()->FsSession();

		const TInt allocRes = aUnicodeData.Create(gbkUncompressData.Length() * 3);	
		
		if ( KErrNone == allocRes)
		{
			CCnvCharacterSetConverter* gbkToUnicodeLE = CCnvCharacterSetConverter::NewLC();
			gbkToUnicodeLE->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk, fs);

			TInt state = CCnvCharacterSetConverter::KStateDefault;
			const TInt convertRes = gbkToUnicodeLE->ConvertToUnicode(aUnicodeData, gbkUncompressData, state);

			if(KErrNone != convertRes)
			{
				isUncompressOK = EFalse;
			}

			/**release**/
			CleanupStack::PopAndDestroy(gbkToUnicodeLE);
		}
		else
		{
			isUncompressOK = EFalse;
		}
			
	}

	CleanupStack::PopAndDestroy(&gbkUncompressData);

	return isUncompressOK;
}