alphaleonis / AlphaFS

AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.
http://alphafs.alphaleonis.com/
MIT License
558 stars 99 forks source link

BackupWrite Native method return (13) the data is invalid #525

Open MustafaDaoud90 opened 3 years ago

MustafaDaoud90 commented 3 years ago

When use backupFileStream writer to create .zip file and add files inside it using Xceed.Zip.ReaderWriter.ZipWriter the BackupWrite native method return (13) the data is invalid. The code sample below :

private void createZipFile(string _path) { try { FileInfo info = new FileInfo(_path);

Xceed.Zip.ReaderWriter.ZipItemLocalHeader oZipEntry = null; string sZipFileGUID = Guid.NewGuid().ToString(); if (!Directory.Exists(info.DirectoryName)) Directory.CreateDirectory(info.DirectoryName);

        Alphaleonis.Win32.Filesystem.BackupFileStream backupFileStream = new Alphaleonis.Win32.Filesystem.BackupFileStream(info.DirectoryName + Path.DirectorySeparatorChar + sZipFileGUID + ".zip", System.IO.FileMode.Create, System.Security.AccessControl.FileSystemRights.FullControl);

        oZipOutputStream = new Xceed.Zip.ReaderWriter.ZipWriter(backupFileStream);

        using (Alphaleonis.Win32.Filesystem.BackupFileStream oReader = new Alphaleonis.Win32.Filesystem.BackupFileStream(_path, System.IO.FileMode.Open, System.Security.AccessControl.FileSystemRights.Read, System.IO.FileShare.Read))
        {
           Xceed.Zip.ReaderWriter.ZipItemLocalHeader localHeader = new Xceed.Zip.ReaderWriter.ZipItemLocalHeader();
           localHeader.CompressionLevel = Xceed.Compression.CompressionLevel.None;
           localHeader.CompressionMethod = Xceed.Compression.CompressionMethod.Deflated;
           localHeader.FileName = @"\" + info.Name;
           localHeader.LastWriteDateTime = DateTime.Now;
           oZipOutputStream.WriteItemLocalHeader(localHeader);

           byte[] buffer = new byte[65536];
           int read = 0;
           while (true)
           {
              if (oZipOutputStream != null)
              {
                 read = oReader.Read(buffer, 0, buffer.Length);
                 if (read <= 0)
                    break;
                 //Here when call WriteItemData the backupFileStream call write method and exception return:  (13) the data is invalid.
                 oZipOutputStream.WriteItemData(buffer, 0, read);
              }
           }
        }

        if (oZipOutputStream != null)
        {
           oZipOutputStream.CloseZipFile();
        }
      }
     catch (Exception ex)
     {
        MessageBox.Show(ex.Message);
     }

}