none
(c#) Create a Zip file from a Text file - NET Framework 3.5 ? RRS feed

  • Question

  • I want to create a Zip file from a Text file using NET Framework 3.5.

    I used the following code. And it works...
    using ICSharpCode.SharpZipLib.Core;
    using ICSharpCode.SharpZipLib.Zip;
    
    private void ZIPToolStripMenuItem_Click(object sender, EventArgs e)
            {
                string path1 = @"E:\Folder1\Folder2\Text Document.txt";
                string path2 = @"E:\Text Document.zip";    
                ZipFile z = ZipFile.Create(path2);     
                z.BeginUpdate();
                z.Add(path1);
                z.CommitUpdate();
                z.Close();
            }
    But problem is when I open the Zip file, I have to open Folder1, then Folder2 ...Text Document.txt
    I wish that when I open a Zip file, it only has a Text file inside.
    Is it possible? If so, my question is how to do it ? 
    I ask for your help. Thanks in advance.

    The result I don't want:



    The result I want, if possible:

    Monday, January 13, 2020 7:35 PM

Answers

  • See if the following would work for you to create a zip without folders.

    https://stackoverflow.com/questions/4924398/how-to-compress-only-files-without-folders-using-the-sharpziplib-library-c


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by nextb88 Tuesday, January 14, 2020 8:44 AM
    Monday, January 13, 2020 7:44 PM
    Moderator
  • Hi nextb88,

    Thank you for posting here.

    You can use the following code to create a compressed package without path.

                string path1 = @"D:\test\txt\test.txt";
                string path2 = @"D:\test\ZIP\test.zip";
                ZipFile z = ZipFile.Create(path2);
    
                StaticDiskDataSource source = new StaticDiskDataSource(path1);
                ZipEntry entry = new ZipEntry(Path.GetFileName(path1));
    
                z.BeginUpdate();
                z.Add(source,entry);
                z.CommitUpdate();
                z.Close();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by nextb88 Tuesday, January 14, 2020 8:37 AM
    Tuesday, January 14, 2020 3:06 AM

All replies

  • See if the following would work for you to create a zip without folders.

    https://stackoverflow.com/questions/4924398/how-to-compress-only-files-without-folders-using-the-sharpziplib-library-c


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by nextb88 Tuesday, January 14, 2020 8:44 AM
    Monday, January 13, 2020 7:44 PM
    Moderator
  • Hi nextb88,

    Thank you for posting here.

    You can use the following code to create a compressed package without path.

                string path1 = @"D:\test\txt\test.txt";
                string path2 = @"D:\test\ZIP\test.zip";
                ZipFile z = ZipFile.Create(path2);
    
                StaticDiskDataSource source = new StaticDiskDataSource(path1);
                ZipEntry entry = new ZipEntry(Path.GetFileName(path1));
    
                z.BeginUpdate();
                z.Add(source,entry);
                z.CommitUpdate();
                z.Close();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by nextb88 Tuesday, January 14, 2020 8:37 AM
    Tuesday, January 14, 2020 3:06 AM
  • Timon thank you so much. That was helpful. My problem is solved. Best regards.
    Tuesday, January 14, 2020 8:44 AM