port from perforce
This commit is contained in:
79
aiwaz/Backup/Aiwaz.Common/FileSystem.cs
Normal file
79
aiwaz/Backup/Aiwaz.Common/FileSystem.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Ionic.Zip;
|
||||
using System.IO;
|
||||
|
||||
namespace Aiwaz.Common
|
||||
{
|
||||
public class FileSystem : IDisposable
|
||||
{
|
||||
private ZipFile zipFile;
|
||||
public FileSystem()
|
||||
{
|
||||
if (File.Exists("data.pak"))
|
||||
{
|
||||
zipFile = ZipFile.Read("data.pak");
|
||||
}
|
||||
}
|
||||
|
||||
~FileSystem()
|
||||
{
|
||||
this.Dispose();
|
||||
}
|
||||
|
||||
public Stream Open(string argFileName)
|
||||
{
|
||||
if (File.Exists(argFileName))
|
||||
{
|
||||
Stream fileStream = new FileStream(argFileName, FileMode.Open);
|
||||
if (fileStream != null)
|
||||
return fileStream;
|
||||
}
|
||||
|
||||
if (zipFile != null)
|
||||
{
|
||||
var file = zipFile[argFileName];
|
||||
if (file != null)
|
||||
{
|
||||
Stream outputStream = new MemoryStream();
|
||||
file.Extract(outputStream);
|
||||
return outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
throw new FileNotFoundException(argFileName);
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (zipFile != null)
|
||||
{
|
||||
zipFile.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
public static class BinaryReaderExtensions
|
||||
{
|
||||
public static string ReadNullTerminatedString(this BinaryReader reader)
|
||||
{
|
||||
string result = string.Empty;
|
||||
char readChar = '\0';
|
||||
do
|
||||
{
|
||||
readChar = reader.ReadChar();
|
||||
if (readChar != '\0')
|
||||
result += readChar;
|
||||
}
|
||||
while (readChar != '\0');
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user