alphaleonis / AlphaFS

AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.

Home Page:http://alphafs.alphaleonis.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BackupWrite Native method return (13) the data is invalid

MustafaDaoud90 opened this issue · comments

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);
     }

}