NBTExplorer/Substrate/SubstrateCS/Source/NBTFile.cs

71 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ionic.Zlib;
namespace Substrate
{
using NBT;
public class NBTFile
{
protected string _filename;
public NBTFile (string path)
{
_filename = path;
}
public bool Exists ()
{
return File.Exists(_filename);
}
public bool Delete ()
{
File.Delete(_filename);
return true;
}
public Stream GetChunkDataInputStream ()
{
FileStream fstr = new FileStream(_filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
long length = fstr.Seek(0, SeekOrigin.End);
fstr.Seek(0, SeekOrigin.Begin);
byte[] data = new byte[length];
fstr.Read(data, 0, data.Length);
fstr.Close();
return new GZipStream(new MemoryStream(data), CompressionMode.Decompress);
}
public Stream GetChunkDataOutputStream ()
{
return new ZlibStream(new NBTBuffer(this), CompressionMode.Compress);
}
class NBTBuffer : MemoryStream
{
private NBTFile file;
public NBTBuffer (NBTFile c)
: base(8096)
{
this.file = c;
}
public override void Close ()
{
FileStream fstr = new FileStream(file._filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
fstr.Write(this.GetBuffer(), 0, (int)this.Length);
fstr.Close();
}
}
}
}