0
我想寫一個類到一個文件並從文件打開類。這適用於我;但是,文件大小非常大。 (65 MB,在.rar中壓縮爲1MB)。序列化和壓縮類到文件
這讓我有理由認爲我可以在寫入文件之前壓縮數據。
我原來的功能是;
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;
}
要壓縮我嘗試以下,但是這似乎並沒有正常工作加載到系統級時:
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;
}
我使用的DeflateStream不正確?我能做些什麼來完成這項工作?
謝謝,這是有道理的。以前從未使用過DeflateStream! – Sliver2009