Last active
February 18, 2022 15:40
-
-
Save DreamVB/64bae2b4b6f50e92574ff517c404f586 to your computer and use it in GitHub Desktop.
Combine multiple files into single file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here is a simple class to pack multiple files into one single file. | |
// it does not support writeing the files dictionary structure as of yet | |
// This was made as I needed something quick to pack many csv files. | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
namespace FilePack | |
{ | |
class bjPak | |
{ | |
//Each file info | |
public struct TFile | |
{ | |
//Filename | |
public string Filename; | |
//Lenfth of the file | |
public int flen; | |
//The files data | |
public byte[] data; | |
} | |
//Holds each files info | |
private List<TFile> FileFrmt; | |
private List<string> Files = new List<string>(); | |
/// <summary> | |
/// Return the number of files | |
/// </summary> | |
public int FileCount | |
{ | |
get | |
{ | |
return FileFrmt.Count; | |
} | |
} | |
/// <summary> | |
/// Remove all file info from FileFrmt List | |
/// </summary> | |
public void ClearFiles() | |
{ | |
FileFrmt.Clear(); | |
} | |
/// <summary> | |
/// Add an array of file names to the Files List | |
/// </summary> | |
/// <param name="_files"></param> | |
public void AddFiles(string[] _files) | |
{ | |
foreach (string lzFile in _files) | |
{ | |
//Check if file is found | |
if (!File.Exists(lzFile)) | |
{ | |
throw new Exception("File Not Found:\n" + lzFile); | |
} | |
else | |
{ | |
//Add to List | |
Files.Add(lzFile); | |
} | |
} | |
} | |
/// <summary> | |
/// Add a single file to the files List | |
/// </summary> | |
/// <param name="Filename"></param> | |
public void AddFile(string Filename) | |
{ | |
//Check file is found | |
if (!File.Exists(Filename)) | |
{ | |
throw new Exception("File Not Found:\n" + Filename); | |
} | |
else | |
{ | |
//Add to files list | |
Files.Add(Filename); | |
} | |
} | |
/// <summary> | |
/// Return File info record | |
/// </summary> | |
/// <param name="index"></param> | |
/// <returns></returns> | |
public TFile GetFile(int index) | |
{ | |
return FileFrmt[index]; | |
} | |
/// <summary> | |
/// Pack all the files in Files list into a single file | |
/// </summary> | |
/// <param name="Filename"></param> | |
public void CreatePack(string Filename) | |
{ | |
try | |
{ | |
using (var fs = File.Create(Filename)) | |
{ | |
using (BinaryWriter br = new BinaryWriter(fs)) | |
{ | |
foreach (string lzFile in Files) | |
{ | |
//Set for next file | |
br.Write(true); | |
//Extract filename only supported at the moment | |
FileInfo fi = new FileInfo(lzFile); | |
//Write filename | |
br.Write(fi.Name); | |
//Get file data | |
byte[] data = File.ReadAllBytes(lzFile); | |
//Write the length of the file | |
br.Write(data.Length); | |
//Write file data bytes to the file | |
br.Write(data); | |
} | |
br.Write(false); | |
br.Close(); | |
} | |
fs.Close(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
/// <summary> | |
/// Opens a pack file and read the contents | |
/// </summary> | |
/// <param name="Filename"></param> | |
public void OpenPack(string Filename) | |
{ | |
TFile f = new TFile(); | |
try | |
{ | |
//Check if file not found | |
if (!File.Exists(Filename)) | |
{ | |
throw new Exception("File Not Found:\n" + Filename); | |
} | |
else | |
{ | |
FileFrmt = new List<TFile>(); | |
using (var fs = File.OpenRead(Filename)) | |
{ | |
using (BinaryReader br = new BinaryReader(fs)) | |
{ | |
//While we have files read and write | |
while (br.ReadBoolean() == true) | |
{ | |
//Get filename | |
string lzFile = br.ReadString(); | |
f.Filename = lzFile; | |
//Read file length | |
f.flen = br.ReadInt32(); | |
//Resize and set data with the file bytes | |
byte[] data = new Byte[f.flen]; | |
//Read the bytes | |
data = br.ReadBytes(f.flen); | |
//Set f.data to bytes | |
f.data = data; | |
//Add to FileFrmt record | |
FileFrmt.Add(f); | |
} | |
br.Close(); | |
} | |
fs.Close(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Windows.Forms; | |
using System.IO; | |
namespace FilePack | |
{ | |
public partial class frmmain : Form | |
{ | |
public frmmain() | |
{ | |
InitializeComponent(); | |
} | |
private void cmdPack_Click(object sender, EventArgs e) | |
{ | |
bjPak pak = new bjPak(); | |
string[]files = Directory.GetFiles(@"C:\out\"); | |
try | |
{ | |
//Add all files in the folder | |
pak.AddFiles(files); | |
//Place the pack file in the folder. | |
//Ps make sure the file is created first as I nt added createing folders as yet | |
pak.CreatePack(@"C:\out\bak\backup.pak"); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
private void cmdUnpack_Click(object sender, EventArgs e) | |
{ | |
string sPakFile = @"c:\out\bak\backup.pak"; | |
string sOutput = @"C:\backup\"; | |
bjPak pak = new bjPak(); | |
try | |
{ | |
//Open pack file | |
pak.OpenPack(sPakFile); | |
//Write each file in the pack to the backup folder | |
for (int x = 0; x < pak.FileCount; x++) | |
{ | |
File.WriteAllBytes(sOutput + pak.GetFile(x).Filename, | |
pak.GetFile(x).data); | |
} | |
pak.ClearFiles(); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment