Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET How Do I Rename The Files Within A ZIP File When Extracting?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20369 |

    Hello,

    I am trying to figure out how to do the following. I need to unzip a file into a directory. If the ZIP file has been saved with folder files inside folders, I need to ignore all of the paths. As each file is extracted from the zip, I need to save it with a name of my choosing.

    For example, if there was a ZIP file saved with these 3 files with these original paths:

    c:\MyDir\abc.txt
    C:\MyDir\xyx.txt
    C:\AnotherDir\SubDir\abc.txt

    I want to write out the 3 files to C:\NewDir without any subdirectories and with new names like this:

    C:\NewDir\1.txt
    C:\NewDir\2.txt
    C:\NewDir\3.txt

    Thanks in advance for your help.

    Imported from legacy forums. Posted by Robert (had 1767 views)

    User (Old forums)
    Member
    Post count: 23064

    I should also state that I am using C#.  Thanks!

    Imported from legacy forums. Posted by Robert (had 245 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,

     I figured out how to change the names of the files within the zip.  I did it like this:

    AbstractFile [] fileList = zip.GetFiles(true, fileMask);
    int numFileInZip = fileList.Length;

    for (int current = 0 ; current < numFileInZip; current++)
    {
      fileList[current].Name = “newname” + current.ToString();  // just a rough example.
    }

     I am still trying to figure out how to unzip into a single folder, removing any subdirectories.

    Thanks.

    Imported from legacy forums. Posted by Robert (had 246 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Robert, 

     I have tried this on my machine, and it would work if you create a recursive method over the folders and moving all files to one location. 

    Here’s a code snippet :

     

      public void RecursiveMoveFiles(AbstractFolder From, AbstractFolder To)

            {         

                foreach (FileSystemItem childItem in From.GetItems(false))

                {

                    if(  typeof(DiskFolder) == childItem.GetType())

                    {

                        var f = (AbstractFolder)childItem;

                        RecursiveMoveFiles(f, To);

               }

                    else

                    {

                        childItem.Name = name.ToString() + “.txt”;

                        childItem.MoveTo(To, true);

                        name++;

                                 }

                }

            } 

     

    This should solve the problem 🙂 

    Imported from legacy forums. Posted by Michel [Xceed] (had 1359 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.