[AccessD] Sample .accdb - three posts (Part 3) - Re[2]: VBA7?
Salakhetdinov Shamil
mcp2004 at mail.ru
Wed Feb 10 10:17:12 CST 2016
Hi Lambert --
Here is the sample C# code to decode and unzip sample .accdb - the code can be run using e.g. LINQPad ( http://www.linqpad.net/ ) - free.
// uncomment if using within VS
//using System;
//using System.IO;
//using System.Text;
//using System.IO.Compression;
//using System.Linq;
void Main()
{
// the folowing commented two code lines were used to prepare the sample .accdb
// zipped & base64 encoded
// string accdbFullPath = @"s:\projects\databases\QdfTestDBs.accdb";
// CompressEncodeToBase64AndSave(accdbFullPath);
// combine two parts of posted base64 file and use the folowing two code lines to restore it into .accdb
string b64FileFullPath = @"s:\projects\databases\QdfTestDBs.b64.txt"; // type you fullpath here
DecodeFromBase64DecompressAndSave(b64FileFullPath);
}
public void CompressEncodeToBase64AndSave(string sourceFileFullPath, string suffix = "")
{
var sourceFileBytes = System.IO.File.ReadAllBytes(sourceFileFullPath);
var sourceFileBytesZipped = Compress(sourceFileBytes);
var sourceFileBytesZippedAndBase64Encoded =
System.Convert.ToBase64String(sourceFileBytesZipped, 0, sourceFileBytesZipped.Length);
var lines = new List<string>();
const int B64_LINE_LENGTH = 64;
for (int i = 0; i < sourceFileBytesZippedAndBase64Encoded.Length; i+= B64_LINE_LENGTH)
{
lines.Add (sourceFileBytesZippedAndBase64Encoded.Substring(i,
Math.Min(B64_LINE_LENGTH, sourceFileBytesZippedAndBase64Encoded.Length-i) ));
}
System.IO.File.WriteAllLines(MakeFullPath(sourceFileFullPath, ".b64.txt", suffix), lines);
}
public string MakeFullPath(string sourceFileFullPath, string newFileExtensionWithLeadingDot, string suffix = "_V2")
{
var folder = System.IO.Path.GetDirectoryName(sourceFileFullPath);
var fileNameNoExt = System.IO.Path.GetFileNameWithoutExtension(sourceFileFullPath);
return System.IO.Path.Combine(folder, fileNameNoExt + suffix + newFileExtensionWithLeadingDot);
}
public void DecodeFromBase64DecompressAndSave(string sourceFileFullPath)
{
var lines = System.IO.File.ReadAllLines(sourceFileFullPath);
var sourceFileBytesZippedAndBase64Encoded =
new StringBuilder(string.Join("",
lines.Where(x => !x.StartsWith("---") && !string.IsNullOrWhiteSpace(x))
.ToArray()));
var sourceFileBytesZipped = System.Convert.FromBase64String(sourceFileBytesZippedAndBase64Encoded.ToString());
var sourceFileBytes = Decompress(sourceFileBytesZipped);
System.IO.File.WriteAllBytes(MakeFullPath(sourceFileFullPath, ".accdb"), sourceFileBytes);
}
public byte[] Compress(byte[] raw)
{
using (MemoryStream memory = new MemoryStream())
{
using (System.IO.Compression.GZipStream gzip =
new System.IO.Compression.GZipStream(memory, System.IO.Compression.CompressionMode.Compress, true))
{
gzip.Write(raw, 0, raw.Length);
}
return memory.ToArray();
}
}
public byte[] Decompress(byte[] gzip)
{
using (System.IO.Compression.GZipStream stream =
new System.IO.Compression.GZipStream(new MemoryStream(gzip),
System.IO.Compression.CompressionMode.Decompress))
{
const int size = 4096;
byte[] buffer = new byte[size];
using (MemoryStream memory = new MemoryStream())
{
int count = 0;
do
{
count = stream.Read(buffer, 0, size);
if (count > 0)
{
memory.Write(buffer, 0, count);
}
}
while (count > 0);
return memory.ToArray();
}
}
}
Thank you.
-- Shamil
>Wednesday, February 10, 2016 3:00 PM UTC from "Heenan, Lambert" <Lambert.Heenan at aig.com>:
>
>Curious. Certainly not working with Access 2010 accdb files.
>
>At least not on my end.
>
>Lambert
>
>
More information about the AccessD
mailing list