• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            We do not always find visible happiness in proportion to visible virtue

            夢幻白樺林

            SHARE

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              14 Posts :: 58 Stories :: 62 Comments :: 0 Trackbacks

            公告

            常用鏈接

            留言簿(5)

            搜索

            •  

            最新隨筆

            最新評論

            閱讀排行榜

            對于一個文件的讀寫,其實很簡單,就是用FileStream進行Read或者Write就行了。但是如何把多個文件寫入到同一個文件,之后要能把這個文件進行還原成多個文件。那么光靠FileStreamReadWrite方法是不夠的,首先你需要自行建立文件索引,來標明每個文件在當前文件的位置。

             

            那么最近作了一個簡單的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>

                /// Summary description for clsComposeFiles.

                /// </summary>

                public class clsComposeFiles

                {

                    private ArrayList arrFiles = new ArrayList();

             

                    public clsComposeFiles()

                    {

                        //

                        // TODO: Add constructor logic here

                        //

                    }

             

                    /// <summary>

                    /// Add a file to be composed

                    /// </summary>

                    /// <param name="sFileName"></param>

                    public void AddFile( string sFileName )

                    {

                        arrFiles.Add( sFileName );

                    }

             

                    /// <summary>

                    /// Compose files to the specific file

                    /// </summary>

                    /// <param name="sFileName"></param>

                    /// <returns></returns>

                    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;

                    }

             

                    /// <summary>

                    /// Write file name and data into composed file

                    /// </summary>

                    /// <param name="sFileName"></param>

                    /// <param name="FileStartPos"></param>

                    /// <param name="IndexStartPos"></param>

                    /// <param name="fsWriter"></param>

                    /// <param name="Index"></param>

                    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;

                    }

             

                    /// <summary>

                    /// Decompose file to multi files into specific directory

                    /// </summary>

                    /// <param name="sFileName"></param>

                    /// <param name="sDestDir"></param>

                    /// <returns></returns>

                    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;

                    }

             

                    /// <summary>

                    /// Save every file into directory

                    /// </summary>

                    /// <param name="fsReader"></param>

                    /// <param name="FI"></param>

                    /// <param name="sDestDir"></param>

                    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;

                            }

                            else

                            {

                                nRealRead = fsReader.Read( bBuffer, 0, (int)lLeft );

                                fsWriter.Write( bBuffer, 0, nRealRead );

             

                                lLeft -= nRealRead;

                            }

                        }

                        while( lLeft > 0 );

             

                        // close file writer

                        fsWriter.Close();

                    }

                }

             

                /// <summary>

                /// File index data structure

                /// </summary>

                public struct FileIndex

                {

                    public long NameStartPos;

                    public long NameLength;

                    public long FileStartPos;

                    public long FileLength;

             

                    public static byte[] ConvertToBytes( ref FileIndex FI  )

                    {

                        byte[] bData = new byte[32];

             

                        Array.Copy( LongToBytes( FI.NameStartPos ), 0, bData, 0, 8 );

                        Array.Copy( LongToBytes( FI.NameLength ), 0, bData, 8, 8 );

                        Array.Copy( LongToBytes( FI.FileStartPos ), 0, bData, 16, 8 );

                        Array.Copy( LongToBytes( FI.FileLength ), 0, bData, 24, 8 );

             

                        return bData;

                    }

             

                    public static byte[] LongToBytes( long lValue )

                    {

                        byte[] bData = new byte[8];

               

                        bData[0] = (byte)( ( lValue >> 56 ) & 0xFF);

                        bData[1] = (byte)( ( lValue >> 48 ) & 0xFF);

                        bData[2] = (byte)( ( lValue >> 40 ) & 0xFF);

                        bData[3] = (byte)( ( lValue >> 32 ) & 0xFF);

                        bData[4] = (byte)( ( lValue >> 24 ) & 0xFF);

                        bData[5] = (byte)( ( lValue >> 16 ) & 0xFF);

                        bData[6] = (byte)( ( lValue >> 8 ) & 0xFF);

                        bData[7] = (byte)(lValue & 0xFF);

                        return bData;

                    }

             

                    public static FileIndex ConvertToFileIndex( byte[] bData )

                    {

                        if( bData == null || bData.Length != 32 )

                            throw new Exception( "Invalid parameters!" );

             

                        FileIndex FI = new FileIndex();

                        byte[] bBuffer = new byte[8];

                        Array.Copy( bData, 0, bBuffer, 0, 8 );

                        FI.NameStartPos = BytesToLong( bBuffer );

                        Array.Copy( bData, 8, bBuffer, 0, 8 );

                        FI.NameLength = BytesToLong( bBuffer );

                        Array.Copy( bData, 16, bBuffer, 0, 8 );

                        FI.FileStartPos = BytesToLong( bBuffer );

                        Array.Copy( bData, 24, bBuffer, 0, 8 );

                        FI.FileLength = BytesToLong( bBuffer );

                        return FI;

                    }

             

                    public static long BytesToLong( byte[] bData )

                    {

                        if( bData == null || bData.Length != 8 )

                            throw new Exception( "Invalid parameters!" );

             

                        long lngValue = 0;

                        lngValue += bData[0];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[1];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[2];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[3];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[4];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[5];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[6];

                        lngValue = ( lngValue << 8 );

                        lngValue += bData[7];

                        return lngValue;

                    }

                }

            }

             

            其中類的操作參看clsComposeFiles這個類,而文件索引結構參看FileIndex這個Structure

             

            之后的調用就很簡單,例如:

            合成文件:

                clsComposeFiles myComposeFiles = new clsComposeFiles();

                myComposeFiles.AddFile( @"D:\Ship.exe" );

                myComposeFiles.AddFile( @"D:\LoginPage.JPG" );

            myComposeFiles.ComposeFiles( @"D:\Ship.dat" );

             

            分解文件:

                clsComposeFiles myComposeFiles = new clsComposeFiles();

                myComposeFiles.DecomposeFile( @"D:\Ship.dat", @"E:\" );
            posted on 2007-10-08 11:50 colys 閱讀(446) 評論(0)  編輯 收藏 引用 所屬分類: C#.Net

            97r久久精品国产99国产精| 一本一本久久aa综合精品 | 久久99精品国产麻豆婷婷| 欧美综合天天夜夜久久| 色婷婷狠狠久久综合五月| 亚洲精品无码专区久久久 | 欧美大香线蕉线伊人久久| 四虎国产永久免费久久| 久久久无码精品亚洲日韩蜜臀浪潮| 一本色综合网久久| 久久久久久国产精品无码下载 | 久久影院午夜理论片无码 | 久久性生大片免费观看性| 久久夜色精品国产噜噜亚洲AV| 中文字幕一区二区三区久久网站| 亚洲一级Av无码毛片久久精品| 精品久久香蕉国产线看观看亚洲| 久久久久亚洲国产| 久久天天躁狠狠躁夜夜av浪潮 | 性做久久久久久久久| 久久国产成人精品麻豆| 少妇人妻综合久久中文字幕| 久久成人国产精品一区二区| 97久久精品人妻人人搡人人玩| 久久久亚洲裙底偷窥综合| 色悠久久久久久久综合网| 99热热久久这里只有精品68| 99久久国语露脸精品国产| 亚洲国产精品无码久久久不卡| 久久91精品国产91| 亚洲欧美一级久久精品| 久久久久婷婷| 国产午夜精品久久久久九九电影| 久久精品国产亚洲欧美| 91精品国产9l久久久久| 91精品国产乱码久久久久久| 亚洲国产精品久久久天堂| 亚洲中文字幕无码久久综合网| 中文成人无码精品久久久不卡| 一本综合久久国产二区| 蜜臀久久99精品久久久久久|