Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Xceed.Zip Empty Folder Extraction

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

    Hi,

     I am trying a scenario where a zip file is zipped using ‘Java.util.zip’ and it contains certain empty folders. Now when i try to unzip the same using Xceed.zip, all those empty folders are not extracted. If I use Winzip for the same operation i get the empty folders.

    Can you suggest some options how to extract empty folders? My idea is to retain the folder structure after extraction happens.

    I have used the below code:

    using Xceed.Zip;
    using Xceed.FileSystem; 

    static void Main(string[] args)
    {
        ExtractZip(“C:\\ZipByCode.zip”, “C:\\ZipByCode”, “”, “”);
    }

    /// <summary>
    /// Extracts the contents of a zip file to a specified folder, based on a file mask (wildcard).
    /// </summary>
    /// <param name=”zipFilename”>Name of the zip file. The file must exist.</param>
    /// <param name=”destFolder”>Folder into which the files should be extracted.</param>
    /// <param name=”fileMask”>Wildcard for filtering the files to be extracted.</param>
    public static void ExtractZip(string zipFilename, string destFolder, string fileMask, string password)
    {
        // Create a DiskFile object for the specified zip filename

        DiskFile zipFile = new DiskFile(zipFilename);

        if (!zipFile.Exists)
        {
            Console.WriteLine(“The specified zip file does not exist.”);
            return;
        }

        Console.WriteLine(“Extracting all files matching the mask \”{0}\” to \”{1}\”…”, fileMask, destFolder);
        Console.WriteLine();

        // Create a ZipArchive object to access the zipfile.

        ZipArchive zip = new ZipArchive(zipFile);

        // Create a DiskFolder object for the destination folder
        DiskFolder destinationFolder = new DiskFolder(destFolder);

        // Create a FileSystemEvents object for handling the ItemProgression event

        //FileSystemEvents events = new FileSystemEvents();
        ZipEvents events = new ZipEvents();

        // Subscribe to the ItemProgression event

        events.ItemProgression += new ItemProgressionEventHandler(OnItemProgression);
        events.ItemException += new ItemExceptionEventHandler(OnItemException);

        try
        {
            // Copy the contents of the zip to the destination folder.
            zip.CopyFilesTo(events, “Extracting”, destinationFolder, true, true, fileMask);
        }
        catch (Exception ex)
        {
           
        }
    }

    /// <summary>
    /// Handles the ItemProgression event.
    /// </summary>
    /// <param name=”sender”>The object that raised this event.</param>
    /// <param name=”e”>Data related to this event.</param>
    private static void OnItemProgression(object sender, ItemProgressionEventArgs e)
    {
        if ((e.CurrentItem != null) && (e.AllItems.Percent < 100))
        {
            Console.WriteLine(“{0} {1}…”, (string)e.UserData, e.CurrentItem.FullName);
        }
    }

    private static void OnItemException(object sender, ItemExceptionEventArgs e)
    {
        if (e.CurrentItem is ZippedFile)
        {

        }
    }

    Thanks in Advance,

    Jyoti Prakash Dash.

     

    Imported from legacy forums. Posted by Jyoti Prakash (had 2803 views)

    User (Old forums)
    Member
    Post count: 23064

    The CopyFilesTo() method copies files (including paths), and not folders.  You will need to modify your code a bit to extract empty folders.  Instead of using CopyFilesTo() on the ZipArchive, you will need to get the items in the archive, and extract them explicitly.

    So instead of :

     zip.CopyFilesTo(events, “Extracting”, destinationFolder, true, true, fileMask);

     

    Do something like the following : 

          foreach( FileSystemItem item in zip.GetItems( false ) )
          {
            if( item.GetType() == typeof( ZippedFile ) )
            {
              ( ( ZippedFile )item ).CopyTo( destFolder, true );
            }
            else
            {
              ( ( ZippedFolder )item ).CopyTo( destFolder, true );
            }
          }
     

    You will need to increment on it to filter the items you don’t want.

     

    Imported from legacy forums. Posted by André (had 2599 views)

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