SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Packing whole directory into memorystream

Last post 09-08-2008 9:03 PM by JohnReilly. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 09-04-2008 10:30 AM

    Packing whole directory into memorystream

    This works just fine when I' m creating zip file, but when I create MemoryStream that will be written to database I get some errors, the code goes something like this:

    if (progDir.Exists)
     {
            FileInfo[ flist = progDir.GetFiles();
            MemoryStream strp = new MemoryStream();
            int duzinap;
            DateTime vreme;
            using (ZipOutputStream zipOS = new ZipOutputStream(strp))
             {
                  ZipEntry zipEntryPrint = new ZipEntry("print/");
                  zipOS.PutNextEntry(zipEntryPrint);
                  foreach (FileInfo f in flist)
                  {
                       ZipEntry zipEntry = new ZipEntry("print/" + f.Name);
                       zipOS.PutNextEntry(zipEntry);
                       FileStream fsp = new FileStream(f.FullName, FileMode.Open);
                       BinaryReader brp = new BinaryReader(fsp);
                       byte[ binp = brp.ReadBytes(Convert.ToInt32(fsp.Length));
                       zipOS.Write(binp, 0, Convert.ToInt32(fsp.Length));
                       fsp.Close();
                       brp.Close();
                  }
                  vreme = DateTime.Now;
                  duzinap =Convert.ToInt32(strp.Length); 

            }

            byte[ binpz =strp.ToArray();

            and this binpz is the varbinary(MAX) that gets into database, all other files ( single program files that I zip, convert ot byte array and store to database have good format, but this zipped dir is unreadable)

  • 09-04-2008 9:40 PM In reply to

    Re: Packing whole directory into memorystream

    So what are the errors/symptoms?

    Have you looked at the data to see whats wrong?

    Cheers, -jr-

     

  • 09-05-2008 10:15 AM In reply to

    Re: Packing whole directory into memorystream

         I don't get any errors at run time, but when I try to open the file I downloaded from database  I get error message: The compressed(zipped) folder is invalid or corrupted, now if I save output in a zip file, and than serialize the file to database all is well, and that is just what I did, but it's not exactly pretty code. 

    PS. You asked have I looked at the data to see whats wrong.. That is beyond my level of expertise, I don't really know all that much about file formats etc.(If you have time or inclination to take a look at the file I'll be happy to send it and very grateful for your assistance)   Here is the modified code that works:

    if (progDir.Exists)
     {
          FileInfo[ flist = progDir.GetFiles();
          DateTime vreme;
          if (File.Exists("C:\\servis\\win\\bamv\\print.zip")) File.Delete("C:\\servis\\win\\bamv\\print.zip");
          ZipOutputStream zipOS = new ZipOutputStream(File.Create("C:\\servis\\win\\bamv\\print.zip"));
          zipOS.SetLevel(9);
          zipOS.UseZip64 = 0;

          ZipEntry zipEntryPrint = new ZipEntry("print/");
          zipOS.PutNextEntry(zipEntryPrint);

          foreach (FileInfo f in flist)
          {
               ZipEntry zipEntry = new ZipEntry("print/" + f.Name);
               zipOS.PutNextEntry(zipEntry);
               FileStream fsp = new FileStream(f.FullName, FileMode.Open);
               BinaryReader brp = new BinaryReader(fsp);
               byte[ binp = brp.ReadBytes(Convert.ToInt32(fsp.Length));
               zipOS.Write(binp, 0, Convert.ToInt32(fsp.Length));
                fsp.Close();
                brp.Close();
          }
          vreme = DateTime.Now;
          zipOS.Finish();
          zipOS.Close();
         

          FileStream fspz = new FileStream("C:\\servis\\win\\bamv\\print.zip", FileMode.Open);
          BinaryReader brpz = new BinaryReader(fspz);
          byte[ binpz = brpz.ReadBytes(Convert.ToInt32(fspz.Length));
          int duzina = Convert.ToInt32(fspz.Length);

    and than binpz goes to database.
                                        

     

  • 09-08-2008 9:03 PM In reply to

    Re: Packing whole directory into memorystream

    Hi,

    Take the output binary data exactly as your code generates and then write it to a file which archivers powerarchiver, winzip et al can be used to look at/test.

    You are already doing this kind of thing when you download from what you say.

    You can try and get at the innards with a tool like AEX (http://www.geocities.com/licamft) as well once you have a file.   This can give you an insight into whats going on

    If need be we can look at the output of that to see whats wrong.

     Thought: I would check that you are writing exactly whats read in terms of number of bytes rather than assuming all the data has been read.this has been a problem in the past.  I havent had a lot of time to look atyour code sorry. hth, -jr-

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.