NAVIGATION - SEARCH

Data Compression Decompression Between Android And Asp.Net C#

There is always a requirement of faster data transmission over the internet between an android app and asp.net backend server. To accomplish that, we can compress and decompress data and then transmit it over http protocol. This reduces the bandwidth usage as compression can reduce the data size significantly which is almost 60% to 80% reduction in bandwidth cost and faster response time.

Whenever you design an app architecture, always consider using data compression techniques which not only reduces dependency on bandwidth but also helps in your app work in slower internet network speeds. Take for e.g. a news app or a blogging app which shows articles which are heavy on content. Your server can compress this data and then send it to your app or vice-versa.

This article gives you the code to compress and decompress data written in Android and Asp.Net C# which you may use in any of your projects. The code is tested and is in working condition thus you do not need to scout for different sources. Since these are 2 different technologies, you may encounter compatibility issues in Android and Asp.Net C# but our code functions are 100% compatible. You may use these within same technology space as they are crossfunctional compression and decompression methods written in Android Java and Asp.Net C#.

Android Code - write these 2 functions to compress and decompress data

public String CompressData(String INPUTDATA) 
{
	String compressedDATA = "";
	try
	{
		ByteArrayOutputStream outputstream = new ByteArrayOutputStream(INPUTDATA.length());
		GZIPOutputStream zipoutputstream = new GZIPOutputStream(outputstream);
		OutputStreamWriter osw = new OutputStreamWriter(zipoutputstream, StandardCharsets.ISO_8859_1);
		osw.write(INPUTDATA);
		osw.close();
		zipoutputstream.close();
		byte[] compressedbytes = outputstream.toByteArray();
		outputstream.close();
		compressedDATA = new String(compressedbytes, "ISO-8859-1");
	}
	catch(Exception ex)
	{
		compressedDATA = "";
	}
	return compressedDATA;
}

public String DecompressData(String INPUTDATA) 
{
	String decompressedDATA = "";
	try
	{
		ByteArrayInputStream inputstream = new ByteArrayInputStream(INPUTDATA.getBytes("ISO-8859-1"));
		GZIPInputStream zipinputstream = new GZIPInputStream(inputstream);
		InputStreamReader isw = new InputStreamReader(zipinputstream, StandardCharsets.ISO_8859_1);
		BufferedReader br = new BufferedReader(isw);
		StringBuilder SB = new StringBuilder();
		String line = "";
		while((line=br.readLine()) != null)
		{
			SB.append(line);
		}
		br.close();
		isw.close();
		zipinputstream.close();
		inputstream.close();
		decompressedDATA = SB.toString();
	}
	catch(Exception ex)
	{
		decompressedDATA = "";
	}
	return decompressedDATA;
}

Asp.Net Code - write these to functions to compress and decompress data in C#

public String CompressData(string INPUTDATA)
{
	String compressedDATA = "";
	try
	{
		byte[] uncompressedbytes = new byte[INPUTDATA.Length];
		int index = 0;
		foreach (char item in INPUTDATA.ToCharArray())
		{
			uncompressedbytes[index] = (byte)item;
			index++;
		}

		using (MemoryStream memorystream = new MemoryStream())
		{
			using (GZipStream gzipstream = new GZipStream(memorystream, CompressionMode.Compress, true))
			{
				gzipstream.Write(uncompressedbytes, 0, uncompressedbytes.Length);
			}
			byte[] compressedbytes = memorystream.ToArray();
			StringBuilder SB = new StringBuilder(compressedbytes.Length);
			for (int i = 0; i < compressedbytes.Length; i++)
			{
				SB.Append((char)compressedbytes[i]);
			}
			compressedDATA = SB.ToString();
		}
	}
	catch (Exception ex)
	{
		compressedDATA = "";
	}
	return compressedDATA;
}

public String DecompressData(string INPUTDATA)
{
	String decompressedDATA = "";
	try
	{
		byte[] compressedbytes = new byte[INPUTDATA.Length];
		int index = 0;
		foreach (char item in INPUTDATA.ToCharArray())
		{
			compressedbytes[index] = (byte)item;
			index++;
		}

		using (MemoryStream memorystream = new MemoryStream(compressedbytes))
		{
			using (GZipStream gzipstream = new GZipStream(memorystream, CompressionMode.Decompress))
			{
				const int size = 1024;
				byte[] buffer = new byte[size];
				byte[] decompressedbytes;
				using (MemoryStream memory = new MemoryStream())
				{
					int count = 0;
					do
					{
						count = gzipstream.Read(buffer, 0, size);
						if (count > 0)
						{
							memory.Write(buffer, 0, count);
						}
					} while (count > 0);
					decompressedbytes = memory.ToArray();
				}
				StringBuilder SB = new StringBuilder(decompressedbytes.Length);
				for (int i = 0; i < decompressedbytes.Length; i++)
				{
					SB.Append((char)decompressedbytes[i]);
				}
				decompressedDATA = SB.ToString();
			}
		}
	}
	catch (Exception ex)
	{
		decompressedDATA = "";
	}
	return decompressedDATA;
}

The above 2 methods can be used to compress or decompress text files, strings, images. For communication between Android App and Asp.Net server backend, you may refer our article here which talks about data transmission between these 2 technology platforms.

 

Add comment