Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create a basic package to store images ? Like a zip file. All i want is to store 20000 images inside one package. Is easier for my hard disk. Also, I need to be able to enter and get out from that package, to read/write , add or remove files, using c# code. Another file format is .iso that is close to what I want, but is complicated to operate with it. I want something very basic, not complicated. Basic as a library if possible. Is there such a thing? Thank you!

What I have tried:

not relevant in this situation.
Posted
Updated 30-May-18 14:44pm
v2

You can create and manage zip files in .NET, see ZipFile Class (System.IO.Compression)[^].
 
Share this answer
 
Comments
_Q12_ 29-May-18 5:15am    
I tried already zip files. I want something more simple than that. More basic if possible. No archiving needed, only storage. Like a box !!!
Richard MacCutchan 29-May-18 5:25am    
You already have it, it's called a Filesystem.
Use the ZipArchive Class in System.IO.Compression available with .NET 4.5: [^].
Create a zip archive from a directory
ZipFile.CreateFromDirectory

Extract the contents of a zip archive to a directory
ZipFile.ExtractToDirectory

Add new files to an existing zip archive
ZipArchive.CreateEntry

Retrieve a file from a zip archive
ZipArchive.GetEntry

ZipArchive.Entries
Retrieve all the files from a zip archive

ZipArchiveEntry.Open
Open a stream to a single file contained in a zip archive

ZipArchiveEntry.Delete
Delete a file from a zip archive
 
Share this answer
 
v2
Comments
_Q12_ 30-May-18 15:32pm    
To BillWoodruff > thank you for your nice explanation - i wish everybody post like you do. Seriously.
-I like your: << ZipArchiveEntry.Open
Open a stream to a single file contained in a zip archive>>

Another problem: I have .Net 4.0 and I'm working with MVS 2010.
I have "System.IO.Compression", but ZipArchive class does not exist in this version of .Net 4.
I already tried with Ionic.Zip.dll but i dont understand how to open a stream inside that library (like ZipArchiveEntry.Open example).
Thank you very much for your very nice sample code !!!
BillWoodruff 30-May-18 22:32pm    
Can you switch to .NET 4.5 ? It won't break your current code. I am not familiar with Ionic, but there are lots of open-source compression libraries ... check GitHub.
_Q12_ 31-May-18 4:36am    
I can not switch to 4.5 unfortunately.Here are the reasons: My hardware is not that performant. I did install MVS 2017? or something, that was updating itself from internet!, and had some funky business to it that i didnt care less. I had some serious problems using that newer version - i dont recall what it was exactly, but it was something horrible - i almost had to reinstall windows because of it.. i didnt in the end. So I back down to oldie but goldie. I like new shiny software, but I like more the practical and working ones. Also tested by time. Maybe i'm an idiot, but is working fine with what hardware and OS configuration I have. I learned to not stretch more than what I have. :P
This library I find some time ago but never had time for it, I like it very much because its very small size - 435kb and also is free to download from internet! I will look on github as you suggest, and thank you for direction me to it. I am curious how small and efficient those are from github.
BillWoodruff 31-May-18 4:43am    
In my experience: at this point, .NET 4.5 is very stable, as is VS 2017.

Check out the new 'Brotli compression from Google,

https://github.com/google/brotli

and, of course, 7-zip.

Remember that pictures files are already compressed,
'jpeg's lossily, .png's losslessly; typically, there's little space saved by further compressing them.
_Q12_ 31-May-18 5:07am    
Thank you mister BillWoodruff, I will look into these new compressing methods. New for me. I never heard of this brotti, and thank you for taking the time to put here the link to it. Very nice.
Again, i am not using zip for compressing, but for storing.
Like a little box.
Thank you all for your input, it helped me decide and it direction me to this answer I find after many web search battles.

I find a practical solution, but not that efficient as I want it.
Is moving slow-ish when cycling the images from inside a zip file, because it is unpacking each of them. I must re-think the code and unzip all into a stream or some lists. I will see. For now, is working and I am very happy :)

Here is the result I came up with:


C#
//My code so far - not very efficient but is working.
using Ionic.Zip;
using Ionic.Zlib;

        string zipPath = "0Images.zip";
        void CountZipFiles()
        {
            using (ZipFile zip = new ZipFile(zipPath))
            {
                totalzipFiles = zip.Count-1;
            }
        }
        Image emptyImage = Image.FromFile("emptyFemale.jpg");
        void ReadZipImage()
        {
            using (ZipFile zip = new ZipFile(zipPath))
            {
                MemoryStream tempS = new MemoryStream();
                for (int i = 0; i < zip.Count; i++)
                {
                    if (i == countMyZipImages)
                    {
                        label1.Text = zip[i].FileName;
                        if (zip[i].FileName.Contains(".niet"))
                        {
                            pictureBox1.Image = emptyImage;
                        }
                        else
                        {
                            zip[i].Extract(tempS);
                            pictureBox1.Image = Image.FromStream(tempS);
                        }
                    }
                }
            }
        }

        int totalzipFiles = 0, countMyZipImages = 0;
        private void button2_Click(object sender, EventArgs e)
        {
            countMyZipImages--;
            if (countMyZipImages < 0) countMyZipImages = totalzipFiles;
            textBox1.Text = countMyZipImages.ToString();
            ReadZipImage();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            countMyZipImages++;
            if (countMyZipImages > totalzipFiles) countMyZipImages = 0;
            textBox1.Text = countMyZipImages.ToString();
            ReadZipImage();
        }



C#
// and this is a HELP file for later use - hopefully will help others too. ;)

How to add Ionic.Zip.dll in c#.net project and use it:

To add a reference, right click (in Solution Explorer on your project) Reference folder and select Add Reference.
Then browse and add the file Ionic.Zip.dll

//Important to add this using's too after referencing.
using Ionic.Zip;
using Ionic.Zlib;


        private void CreateZIP_Click(object sender, EventArgs e)
        {
            using (ZipFile zip = new ZipFile())
            {
                // add this map file into the "images" directory in the zip archive
                zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
                // add the report into a different directory named "files" in the archive
                zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
                zip.AddFile("ReadMe.txt");
                zip.Save("MyZipFile.zip");
                
                Exception ex = new Exception();
                label1.Text = ex.Message;
            }
        }


		//You can extract to a stream, or a fizical file ! 
        private void button5_Click(object sender, EventArgs e)
        {
            using (ZipFile zip = new ZipFile("0Images.zip"))
            {
                MemoryStream tempS = new MemoryStream(); //stream
                
          //{      
                foreach (ZipEntry ze in zip)		//foreach
                {
                    // check if you want to extract the image.name 
                    if (ze.FileName == "00002 Riley Reid.jpg")
                    {
                        ze.Extract(tempS);
                        pictureBox1.Image = Image.FromStream(tempS);
                    }
                }
           //OR
                for (int i = 0; i < zip.Count; i++)	//for
                {
                    if (i == countMyZipImages)
                    {
                        zip[i].Extract(tempS);
                        pictureBox1.Image = Image.FromStream(tempS);
                    }
                }
           //}     
                
            }
        }


This is a free library I find on internet! I like it because is very little - 435kb. Here is a link I find for others if they want to use it. Dropbox - Ionic.Zip.dll[^]
 
Share this answer
 
v4
Comments
BillWoodruff 30-May-18 22:34pm    
Is there any way to predict which images you will need unpacked and cache them ?
_Q12_ 31-May-18 4:10am    
to BillWoodruff>> I am not very sure what are you having in mind when you say "predict"; but my next step is to make some pages with probably 30 img per page. Remember, I have 12000 or so images(i was having 20000).Hopefully I will be able to show (30)them instantaneous and without any kind of delay (like I have right now). I will probably unzip all this zip file into memory stream, and store them into Image lists. That's what i am thinking at this point. I will see what kk will happen after this negotiation. And probably I will update my answer.
BillWoodruff 31-May-18 4:37am    
By "predict" I mean knowing, through some means, which 30 pictures you want to access next at any moment in time.

I think you should pay careful attention to Richard MacCutchan's advice here: folders are cheap; the file system is fast.

Remember that pictures files are already compressed,
'jpeg's lossily, .png's no-loss; typically, there's little space saved by further compressing them.
Richard MacCutchan 31-May-18 3:50am    
Why are you making things so difficult for yourself? If you just store all these files in the filesystem (maybe in hierarchical subfolders) then the ststem will do all the management. All you need to do is add files to the folders, find them (easy), delete them etc. You could even create a small database is an index.
_Q12_ 31-May-18 4:19am    
to Richard MacCutchan>> What you say is very good and normal for 100 files. But for more than 10000, ugh... My point, that is not that clear, is very simple. When I usually perform a search on my hdd, manual search to find something, semi-automatic search to calculate the size of stuff, antivirus searches, and in general anything that is searching, (ehem, viruses also)... I/them have a long time iterating through all that scrap of 10000files that is not relevant for the search itself, and is freeze there for some time to do it's business properly. This is not a good job at all in my eyes. I like stuff to be neat and efficient. It's how I like it. And also I learn new stuff in the way. I hope it makes sense now for you. If not, dont hesitate to ask me. I am all open source here. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900