Пример #1
0
        //fileID是一个GUID
        public bool AddFile(string sourceFileName, string fileID)
        {
            string tempPath = Application.StartupPath + @"\tempzip";

            CheckTempPath(tempPath);

            string destFileName = tempPath + @"\" + Path.GetFileName(sourceFileName) + ".zip";

            I3SharpZip.ZipFile(sourceFileName, destFileName, 9, 4096);

            FileStream dataFileStream = null;
            FileStream destFileStream = null;

            try
            {
                dataFileStream = new FileStream(_fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
                destFileStream = new FileStream(destFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                if (dataFileStream.Length + destFileStream.Length > int.MaxValue)
                {
                    //MessageBox.Show("增加此文件后,数据文件大小将超过2G,取消增加此文件!");
                    IEFS_Error.LastErrorMessage = "增加此文件后,数据文件大小将超过2G,取消增加此文件!";
                    return(false);
                }
                PutNextEntry(dataFileStream, fileID, Path.GetFileName(sourceFileName), destFileStream.Length);
                byte[] buffer = new byte[4096];
                while (true)
                {
                    int size = destFileStream.Read(buffer, 0, buffer.Length);
                    if (size <= 0)
                    {
                        break;
                    }
                    dataFileStream.Write(buffer, 0, size);
                }
                return(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }
            finally
            {
                if (dataFileStream != null)
                {
                    dataFileStream.Close();
                }
                if (destFileStream != null)
                {
                    destFileStream.Close();
                }
                File.Delete(destFileName);
            }
        }
Пример #2
0
        public bool GetFile(string destFileName, string fileID)
        {
            #region 获取文件信息
            long          pos      = 0;
            IEFS_FileInfo fileInfo = GetFileInfo(fileID, out pos);
            if (fileInfo == null)
            {
                return(false);
            }
            #endregion

            #region 得到临时文件名
            string tempPath = Application.StartupPath + @"\tempzip";
            CheckTempPath(tempPath);
            string tempFileName = tempPath + @"\" + fileID + ".zip";
            #endregion

            #region 从数据文件中提取相应文件的压缩包
            FileStream dataFileStream = null;
            FileStream tempFileStream = null;
            try
            {
                dataFileStream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.None);
                tempFileStream = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
                byte[] buffer = new byte[4096];
                dataFileStream.Seek(pos + fileInfo.BytesLength, SeekOrigin.Begin);
                while (true)
                {
                    int size = dataFileStream.Read(buffer, 0, buffer.Length);
                    if (size <= 0)
                    {
                        break;
                    }
                    if (dataFileStream.Position > (pos + fileInfo.BytesLength + fileInfo.fileLength))
                    {
                        int xsize = size;
                        size -= Convert.ToInt32(dataFileStream.Position - pos - fileInfo.BytesLength - fileInfo.fileLength);
                        if (size == 0)
                        {
                            break;
                        }
                        buffer = new byte[size];
                        dataFileStream.Seek(dataFileStream.Position - xsize, SeekOrigin.Begin);
                        dataFileStream.Read(buffer, 0, buffer.Length);
                        tempFileStream.Write(buffer, 0, buffer.Length);
                        break;
                    }
                    else
                    {
                        tempFileStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                #region 出错处理
                MessageBox.Show(ex.Message);
                return(false);

                #endregion
            }
            finally
            {
                #region 释放
                if (dataFileStream != null)
                {
                    dataFileStream.Close();
                }
                if (tempFileStream != null)
                {
                    tempFileStream.Close();
                }
                #endregion
            }
            #endregion

            #region 从压缩包中解压文件
            I3SharpZip.UpZipFileByIndex(tempFileName, destFileName, 4096, 1);
            return(true);

            #endregion
        }