1

ZipOutputStream 将数据文件附加到现有条目?

美好的一天,asp.net c#,框架 4.0

我使用 Ionic.Zip 压缩数据文件(上传器处理程序)来飞行。

上传大文件。

如果文件上传了全部内容(不是块) - 没关系,但如果文件上传有块......我有问题。

我如何将所有块上传文件添加到存档(一个文件 - 一个条目)?不保存文件本身的驱动器。

对不起我的英语不好。

我的部分代码:

创建长度为 = currents 块的临时文件

using (FileStream fs = new FileStream(Path.Combine(pathToCreate, "chunkfile"), FileMode.Create))
            {
                buffer = new byte[fileUpload.InputStream.Length];
                fileUpload.InputStream.Read(buffer, 0, buffer.Length);
                fs.Write(buffer, 0, buffer.Length);
            }

获取 zip 条目:

using (ZipFile zipfile = ZipFile.Read(fullpath))
{ 
if (zipfile.ContainsEntry(fileName))
{
zipflag = true;
}
}

如果 Entry 存在 - 追加数据,如果不存在,则创建新的:

if (!zipflag)
            {
                using (ZipFile zipfile = ZipFile.Read(fullpath))
                {
                        using ( FileStream input = new FileStream (Path.Combine(pathToCreate, "chunkfile"), FileMode.Open, FileAccess.Read))
                        {
                            using (FileStream output = new FileStream(fullpath, FileMode.Create, FileAccess.Write))
                            {
                                using (ZipOutputStream zipout = new ZipOutputStream(output))
                                {
                                    buffer = new byte[input.Length];
                                    zipout.PutNextEntry(fileName);
                                    int size;
                                    do
                                    {
                                        size = input.Read(buffer, 0, buffer.Length);
                                        zipout.Write(buffer, 0, size);
                                    } while (size > 0);
                                }
                            }
                        }
                }
            }
            else
            {

                    buffer = FileArray(Path.Combine(pathToCreate, "chunkfile"));
                    AppendAllBytes(fullpath, buffer);
             }
        }
public static void AppendAllBytes(string path, byte[] bytes)
{
   using (var stream = new FileStream(path, FileMode.Append))
     {
        using (ZipOutputStream zipout = new ZipOutputStream(path))
            {
               zipout.Write(bytes, 0, bytes.Length);
            }
     }
}
private byte[] FileArray(string filename)
{
   FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
   // Create a byte array of file stream length
   byte[] datachunk = new byte[fs.Length];

   //Read block of bytes from stream into the byte array
   fs.Read(datachunk, 0, System.Convert.ToInt32(fs.Length));

  //Close the File Stream
  fs.Close();
  return datachunk; //return the byte data
}

变量此字符串,Zip 文件的完整路径。

我正在尝试使用:

using (var stream = new FileStream(path, FileMode.Append))
    {
        ZipFile zipfile = ZipFile.Read(path);
        using (StreamReader sr = new StreamReader(path))
        {
            var zn = zipfile.UpdateEntry(fileName, bytes);
            sr.Close();
            sr.Dispose();
        }
    }

无效,文件不更新。

请帮忙。

4

0 回答 0