如何把多个文件写入到一个文件中去并能分解
对于一个文件的读写,其实很简单,就是用FileStream进行Read或者Write就行了。但是如何把多个文件写入到同一个文件,之后要能把这个文件进行还原成多个文件。那么光靠FileStream的Read和Write方法是不够的,首先你需要自行建立文件索引,来标明每个文件在当前文件的位置。
那么最近作了一个简单的DEMO,其中类的部分代码如下:
//------------------------------- Compose Files ----------------------------------
//--------------------------------------------------------------------------------
//---File:clsComposeFiles.cs
//---Description:This file is to show how-to compose multi-files into one file
// and decompose one file to multi-files.
//---Author:Knight
//---Date:May.16, 2006
//--------------------------------------------------------------------------------
//------------------------------{ Compose Files }---------------------------------
namespace ComposeFiles
{
using System;
using System.IO;
using System.Collections;
using System.Text;
///
/// Summary description for clsComposeFiles.
///
public class clsComposeFiles
{
private ArrayList arrFiles = new ArrayList();
public clsComposeFiles()
{
//
// TODO: Add constructor logic here
//
}
///
/// Add a file to be composed
///
///
public void AddFile( string sFileName )
{
arrFiles.Add( sFileName );
}
///
/// Compose files to the specific file
///
///
///
public bool ComposeFiles( string sFileName )
{
if( arrFiles.Count == 0 ) return false;
FileInfo fi = new FileInfo( sFileName );
// Open file to write
FileStream fsWriter = null;
try
{
if( !fi.Exists )
{
fsWriter = new FileStream(
sFileName,
FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.None );
}
else
fsWriter = new FileStream(
sFileName,
FileMode.Truncate,
FileAccess.ReadWrite,
FileShare.None );
}
catch(Exception err)
{
System.Diagnostics.Debug.WriteLine( err.Message );
return false;
}
byte[] bBuffer = null;
// Write files count
bBuffer = FileIndex.LongToBytes( arrFiles.Count );
fsWriter.Write( bBuffer, 0, 8 );
const long INDEX_START_POS = 8L;
// Init files index
FileIndex FI = new FileIndex();
for( int i = 0; i < arrFiles.Count; i++ )
fsWriter.Write( FileIndex.ConvertToBytes( ref FI ), 0, 32 );
long FILE_START_POS = INDEX_START_POS + 32 * arrFiles.Count;
long lCurFileStartPos = FILE_START_POS;
// Write every file
for( int i = 0; i < arrFiles.Count; i++ )
{
WriteFile( arrFiles[i].ToString(),
ref lCurFileStartPos,
INDEX_START_POS,
fsWriter,
i );
}
// Close stream
fsWriter.Close();
return true;
}
///
/// Write file name and data into composed file
///
///
///
///
///
///
private void WriteFile(
string sFileName,
ref long FileStartPos,
long IndexStartPos,
FileStream fsWriter,
int Index )
{
FileInfo fi = new FileInfo( sFileName );
if( !fi.Exists ) return;
FileStream fsReader = null;
try
{
fsReader = new FileStream(
sFileName, FileMode.Open,
FileAccess.Read );
}
catch{ return;}
// Get file name
byte[] bFileName = Encoding.Unicode.GetBytes( fi.Name );
// Write file name
fsWriter.Write( bFileName, 0, bFileName.Length );
const int BUFFER_LENGTH = 1024;
byte[] bBuffer = new byte[BUFFER_LENGTH];
int nRealRead = 0;
// Write data using
do
{
// Read data from file
nRealRead = fsReader.Read( bBuffer, 0,
BUFFER_LENGTH );
// Write data
fsWriter.Write( bBuffer, 0, nRealRead );
}while( nRealRead > 0 );
// Close file reader
fsReader.Close();
FileIndex FINew = new FileIndex();
FINew.NameStartPos = FileStartPos;
FINew.NameLength = bFileName.Length;
FINew.FileStartPos = FileStartPos + bFileName.Length;
FINew.FileLength = fi.Length;
// Go back to file index position
fsWriter.Seek( IndexStartPos + Index * 32, SeekOrigin.Begin );
// Write file index info
fsWriter.Write( FileIndex.ConvertToBytes( ref FINew ), 0, 32 );
// Go back to file end
fsWriter.Seek( 0, SeekOrigin.End );
// Set file current position
FileStartPos += bFileName.Length + fi.Length;
}
///
/// Decompose file to multi files into specific directory
///
///
///
///
public bool DecomposeFile( string sFileName, string sDestDir )
{
FileInfo fi = new FileInfo( sFileName );
if( !fi.Exists ) return false;
FileStream fsReader = null;
try
{
fsReader = new FileStream(
sFileName, FileMode.Open,
FileAccess.Read );
}
catch{ return false;}
// Read file count
byte[] bFileCount = new byte[8];
int nRealRead = 0;
nRealRead = fsReader.Read( bFileCount, 0, 8 );
if( nRealRead != 8 )
{
fsReader.Close();
return false;
}
long lFileCount = FileIndex.BytesToLong( bFileCount );
if( lFileCount > 0 )
{
//Init file index array
FileIndex[] fiArray = new FileIndex[lFileCount];
byte[] bFileIndex = new byte[32];
for( int i = 0; i < lFileCount; i++ )
{
fsReader.Read( bFileIndex, 0, 32 );
fiArray[i] = FileIndex.ConvertToFileIndex( bFileIndex );
}
if( sDestDir[ sDestDir.Length - 1] != '\' )
sDestDir += "\";
// Save every file into current directory
for( int i = 0; i < fiArray.Length; i++ )
{
SaveFile( fsReader,
ref fiArray[i],
sDestDir );
}
}
// Close file reader
fsReader.Close();
return true;
}
///
/// Save every file into directory
///
///
///
///
private void SaveFile(
FileStream fsReader,
ref FileIndex FI,
string sDestDir )
{
// Read file name
byte[] bFileName = new byte[ FI.NameLength ];
int nRealRead = fsReader.Read( bFileName, 0, bFileName.Length );
if( nRealRead != bFileName.Length ) return;
string sFileName = Encoding.Unicode.GetString( bFileName );
sFileName = sDestDir + sFileName;
FileInfo fi = new FileInfo( sFileName );
// Open file to write
FileStream fsWriter = null;
try
{
if( !fi.Exists )
{
fsWriter = new FileStream(
sFileName,
FileMode.CreateNew,
FileAccess.ReadWrite,
FileShare.None );
}
else
fsWriter = new FileStream(
sFileName,
FileMode.Truncate,
FileAccess.ReadWrite,
FileShare.None );
}
catch(Exception err){
System.Diagnostics.Debug.WriteLine( err.Message );
return;
}
// Init buffer
const int BUFFER_LENGTH = 1024;
byte[] bBuffer = new byte[BUFFER_LENGTH];
long lLeft = FI.FileLength;
// Copy file
do
{
if( lLeft > BUFFER_LENGTH )
{
fsReader.Read( bBuffer, 0, BUFFER_LENGTH );
fsWriter.Write( bBuffer, 0, BUFFER_LENGTH );
lLeft -= BUFFER_LENGTH;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130553/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/374079/viewspace-130553/