0

I have a user that wants to upload a zip file that contains multiple images and a single CSV file with data related to the images. They want to be able to upload the zip file and have the program dissect it by finding and processing the data within the CSV file and then storing the images within the zip to their appropriate locations.

I'm trying to figure out how to open the zip so I can cycle through each file in there to find what I need. Is there any way to do this?

EricP.
  • 489
  • 3
  • 21

1 Answers1

1

You can use ZipArchive from Acumatica Framework:

// Uploaded file needs to be attached to a DAC record
Guid[] files = PXNoteAttribute.GetFileNotes(DACCache, DACRecord);
UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();

foreach (Guid fileID in files)
{
    FileInfo fileInfo = upload.GetFile(fileID);

    if (fileInfo != null)
    {
        using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
        {
            ZipArchive zip = ZipArchive.OpenReadonly(stream);

            string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            Directory.CreateDirectory(tempDirectory);
            ZipFolder.Decompress(zip, tempDirectory, true);

            foreach (string filePath in Directory.GetFiles(tempDirectory))
            {
                // Enumerating decompressed files
            }
        }
    }
}
Hugues Beauséjour
  • 8,067
  • 1
  • 9
  • 22
  • Thanks HB. I'll put this into action and let you know the results. – EricP. Jul 10 '18 at 15:52
  • Code is untested and has very little error handling but it should get you started. – Hugues Beauséjour Jul 10 '18 at 15:53
  • The only functions I see on Acumatica's ZipArchive is CreateFrom(). Do I need a reference for OpenReadonly? Also, I cannot find anything that resembles ZipFolder. I was able to get the rest of the syntax working thus far. Any suggestions? P.S. By ZipFolder did you mean ZipFile? – EricP. Jul 10 '18 at 18:24
  • PX.DbServices.ZipArchive and PX.DbServices.ZipFolder, you can probably achieve similar results with PX.Common.ZipArchive but then code would be different than the one I provided. – Hugues Beauséjour Jul 10 '18 at 18:43
  • Thanks HB! That does what I need. Sadly, I forgot to mention that the file is passworded. Is Acumatica capable of sending a password to the zip file when it attempts to open it? – EricP. Jul 10 '18 at 20:06
  • No it isn't, you'll have to use a dedicated Zip library for that like DotNetZip. – Hugues Beauséjour Jul 11 '18 at 14:05
  • Alright, I will get DotNetZip for that task.Thanks and I appreciate the help HB! – EricP. Jul 11 '18 at 14:06