Game Development Reference
In-Depth Information
}
foreach (var file in directoryInfo.GetFiles())
{
FileAttributes attributes = File.GetAttributes(file.FullName);
if ((attributes & FileAttributes.Hidden) == 0)
{
string relativeFromRoot =
file.FullName.Substring(rootDirLen);
Uri relUri = GetRelativeUri(relativeFromRoot);
PackagePart packagePart = package.CreatePart(
relUri, System.Net.Mime.MediaTypeNames.Application.Octet,
CompressionOption.Maximum);
using (FileStream fileStream = new FileStream(file.FullName,
FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePart.GetStream());
}
}
}
}
}
}
The
Create()
method uses an iterative algorithm to walk the entire directory tree.
Any directories it finds, except for those named Editor, are processed. Ignoring any-
thing named Editor allows editor-specific data to be stored in the Assets directory but
excluded from the final Zip file that the game will read with its resource cache. As
you saw previously with the assets tree, hidden files are also excluded.
For each included file, a
PackagePart
object is created and written to the Zip file.
There are three helper methods that are a part of the
ZipFileUtility
class, and all
are called from the
Create
method. The first is
CopyStream
, which reads a target
stream and copies it in chunks to a target stream
—
it can be extremely useful for large
files.
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 16384;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}