How save folder with all its files in one file in binary format?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1684 Views - Last Post: 06 January 2017 - 06:49 PM Rate Topic: -----

#1 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

How save folder with all its files in one file in binary format?

Posted 08 December 2016 - 10:26 PM

I need to save all files, nested folders of specified folder to one file using binary serilization. Then I also need to have feature to unpack this file with folder content to another folder by preserving the previous structure. What I need to do? Use binary formattes and some kind of List<MemoryStream>, and Dictionary <Number of file, File Path> to put the files in correct folder/nested folder? Or it is possible just with Zipping without no compression option/property?

Is This A Good Question/Topic? 0
  • +

Replies To: How save folder with all its files in one file in binary format?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How save folder with all its files in one file in binary format?

Posted 08 December 2016 - 10:42 PM

Binary serialization is not meant to serialize file system structures. Binary serialization is meant to serialize in memory object graphs.

If your objective is to package up a directory structure including files, then zipping is likely going to be your most convenient way of doing things. I don't understand why you would want to disable the compression. Unless you are just trying to copy/move a directory structure from one place to another within the same drive, it will actually be faster to take the CPU/time overhead to perform the compression, transmit, and then unpack on the other end.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,724
  • Joined: 12-December 12

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 01:58 AM

There is a CompressionLevel Enumeration but, as Skydiver explains, this shouldn't really be a concern.
Was This Post Helpful? 0
  • +
  • -

#4 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 03:03 AM

I have read about Zip compression:https://johnlnelson.com/2014/06/21/zip-files-with-c/
But can it be considered as binary serialization?
In case of No Compression level, what is it? Is it binary serialized?
Was This Post Helpful? 0
  • +
  • -

#5 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 03:13 AM

Quote

"Binary serialization is not meant to serialize file system structures. Binary serialization is meant to serialize in memory object graphs"


-- Yes I need to memory graph objects, despite during the deserialization I need to regenerate the same content and file structure, as ir was before.
But the task tells about use binary serialization, no more, no less -- so what would be your suggestions? - as I have no practice with serialization, and C# I/O, but my time to do it extremely matter, no the time of app processing. Indeed during pure serialization the folder with big size probably would incure recursion (stackoverflow) issues. F.I. in Qt C++ there is special class for traversing of file directory, that prevent this problem, that arise when using recursion during handling all files in directory and sub-directories.

This post has been edited by Curtis Rutland: 09 December 2016 - 07:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 06:48 AM

I have just 2 question now:
http://www.dib0.nl/c...one-stream-in-c -- is it valid example to pure serialization of multiple files?
Indeed I have no assumption how to deserialize, how to cast to type of object if we need to get file, taht could to be whatever format and type.
And simple question is using ZipFile.CreateFromDirectory ( System.IO.Compression namespace) with no Compression level an binary serialization.
What is theory about its content after zipping in this way, are they just bytes of previous file content, and what markers seprate them each other?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 07:32 AM

See zip file format. ZIP uses a central directory.

If you want something that simply has filenames and data set sequentially, try looking at the XMODEM, YMODEM, ZMODEM, and FTP transfer protocols.
Was This Post Helpful? 0
  • +
  • -

#8 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 08:29 AM

I have looked at zip-file description from wikipedia and found no mention of serialization, no binary.
There is a nig issue that ZipFile and Compression namespace work just with 4.5 framework when I have just 3.5 and 3.0.
Just referencing to separate dll from internet lead to warning about bad image so there is incompability.
Should this one:http://www.csharpque.com/2013/05/zipfiles-dotnet35-CreateAssembly.html to be replacement for ZIP in 3.5 framework?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How save folder with all its files in one file in binary format?

Posted 09 December 2016 - 11:54 AM

The bad image is likely due to you having a bitness mismatch. Either you have a 32-bit DLL and your C# code is running as 64-bit, or you have a 64-bit DLL and your code is running as 32-bit.

As for that web page about how to emulate the newer .NET Framework support for Zip files. I don't know. For complete ZipFiles, I've only used the newer APIs, and for some of my basic data compression needs where I didn't really need .zip file compatibility, I used the older APIs. Personally, I don't have much confidence in that page because of the Java style coding he is using as well as the various typos within the code.

And of course Wikipedia's article about the zip file makes no mention about serialization because the zip file is all data compression and packaging. Serialization is about taking something in memory and putting it into long term storage or a data pipe so that it can be rehydrated back into memory. Files and directories are already in long term storage. There is no need for serialization.

Can you post the exact text of your assignment? It feels like there is something you maybe misinterpreting.
Was This Post Helpful? 0
  • +
  • -

#10 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 10 December 2016 - 05:49 AM

I have done whole application in wpf exept i cannot use compression namespace and uncomment zipfile.createfromdirectory and zipfile.extracttodirectory. Would anybody get my application add reference, uncomment 2 lines and launch and check compression and uncompression?
Was This Post Helpful? 0
  • +
  • -

#11 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 11 December 2016 - 02:04 PM

I would like to get your advice relating pour binarization as I cannot check it today at practice. So after one option with zipping I want to get real one with binaryformatter, bytes and streams that is probably required. One simple way is probably use all files of folder traversal and creating the two Lists or one Dictionary<String filepath, Filestream file>, as dictionary seems to get any type for value. Than just to deseriale and put every filestream to a new path of Directory.Combine(newfolder, filepath). Other option is probaly exactly I need despite more complex. I create Filestream/MemoryStream object for later binary serialization: at first I add bytes of every processed file to memorystream despite I do not know how correctly append every new byte. memorystream.writeto(ba,0,ba.length). The second object I need is Dictionary between path and position in stream after writing file bytes. How do define this position exactly after using writeto() at concrete iteration of file traversal? Just use something like that: int p=memorystream.position? Them during unpacking I need to copy m.s.position-(m.s.previousfileposition 1). Is it possible to copy such range of bytes and convert it again to file knowing it filepath?
Was This Post Helpful? 0
  • +
  • -

#12 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 12 December 2016 - 02:30 PM

I have practical issue when getting all subfolders of certain folder. I use Directory.GetDirectories(path,"*", AllDirectories). This method returns the string array and when I convert it with .ToList() and them return such list I cannot convert to list or then assign this list to some variable inside main class such as List<String> folders=new List <String>(); folders=listfolders(path); I get error in last string that non-"delivering" (as far as I remember such word or similar around right part of assignment) List<String> ...What could be reason of it? When I assign string array of foldernames to such empty array variable such as String [] folders = new String[] {} I got the same abovementioned error even without List<String>?
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How save folder with all its files in one file in binary format?

Posted 12 December 2016 - 04:35 PM

Post the exact error you are seeing.

Also, it may help if you break up your comments and questions on this forum into appropriate paragraphs and sentences. The giant run-on wall of text gets to be hard to read sometimes.
Was This Post Helpful? 0
  • +
  • -

#14 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How save folder with all its files in one file in binary format?

Posted 13 December 2016 - 02:43 AM

There are also 2 issues. The first one how to avoid unauthorized access exception for some files in particular some system ones. Should I change folder/file accessrules for write access or even reading and opening rigts are enough to convert file content to byte array? And I cannot change permissions in the whole for these files programmaticaly in C#?2. How can I get relative path from folderpath? Just using substring(Length-/Index-1) them I want get some relative path from certain folder inside path?
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6335
  • View blog
  • Posts: 21,747
  • Joined: 05-May 12

Re: How save folder with all its files in one file in binary format?

Posted 13 December 2016 - 07:11 AM

DO NOT CHANGE PERMISSIONS. You likely simply need to run elevated as an admin rather than as a normal user. But even as an admin, you'll still encounter some folders that you won't have access to. You'll need to have the appropriate error handling and decide if you simply need to skip them or error out.

If you are starting bottom up, or at some random location, you can use FileInfo's you can take advantage of FileInfo.Directory to start building a tree structure. Once you have collected all the files and directories you are interested in, you can find the first node in the tree that has multiple children. All the children will be relative to that node.

If you are starting from a given directory selected by the user, then this is just a simple of recursive traversal through all the directories. Each file and directory is relative to that selected directory, and as you recurse into subdirectories, you prepend the directory name to form a relative path.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2