I am trying to write a class to a file and open the class from a file. This works for me; however, the file size is very big. (65 MB, compressed in a .rar is 1MB).
This gave me the reason to think that I could compress the data before writing to the file.
My original functions are;
public static void save(System system, String filePath){
FileStream fs = new FileStream(filePath, FileMode.Create);
try{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, system);
fs.Flush();
}catch(Exception e){
}finally{
fs.Close();
}
}
public static System load(String filePath){
System system = new System();
FileStream fs = new FileStream(filePath, FileMode.Open);
try{
BinaryFormatter bf = new BinaryFormatter();
system = (System)bf.Deserialize(fs);
fs.Flush();
}catch(Exception e){
}finally{
fs.Close();
}
return system;
}
To compress I Tried the following, however this does not seem to work correctly when loading to the system class:
public static void save(System system, String filePath){
FileStream fs = new FileStream(filePath, FileMode.Create);
try{
BinaryFormatter bf = new BinaryFormatter();
DeflateStream cs = new DeflateStream(fs, CompressionMode.Compress);
bf.Serialize(cs, system);
fs.Flush();
}catch(Exception e){
}finally{
fs.Close();
}
}
public static System load(String filePath){
System system = new System();
FileStream fs = new FileStream(filePath, FileMode.Open);
try{
BinaryFormatter bf = new BinaryFormatter();
DeflateStream ds = new DeflateStream(fs, CompressionMode.Decompress);
system = (System)bf.Deserialize(ds);
fs.Flush();
}catch(Exception e){
}finally{
fs.Close();
}
return system;
}
Am I using the DeflateStream incorrectly? What can I do to make this work?