Skip to content

Instantly share code, notes, and snippets.

@jvanhoesen
Created March 28, 2022 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvanhoesen/a7f83bda81022b2aa4580afaff5af77e to your computer and use it in GitHub Desktop.
Save jvanhoesen/a7f83bda81022b2aa4580afaff5af77e to your computer and use it in GitHub Desktop.
Opens a zip file attached to an Acumatica document
public PXAction<ARInvoice> OpenZip;
[PXButton(CommitChanges = false)]
[PXUIField(DisplayName = "Open .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)]
protected virtual IEnumerable openZip(PXAdapter pxAdapter)
{
if(Base.Document.Current != null)
{
//Retrieves the sample .Zip file from the invoice
Guid zipFile = PXNoteAttribute.GetFileNotes(Base.Document.Cache, Base.Document.Current).First();
if(zipFile != null)
{
UploadFileMaintenance uploadFileMaint = PXGraph.CreateInstance<UploadFileMaintenance>();
PX.SM.FileInfo fileInfo = uploadFileMaint.GetFile(zipFile);
if(fileInfo != null)
{
using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
{
//Create new instance of ZipArchive to interact with .ZIP file
using (ZipArchive zip = ZipArchive.CreateFrom(stream, true))
{
//Iterate through all files and retrieve their ItemInfo
foreach(ZipArchive.ItemInfo info in zip.GetFiles())
{
//Creates a stream from the specified file within the .ZIP file
using (Stream fileStream = zip.OpenRead(info.Name))
{
using(MemoryStream innerStream = new MemoryStream())
{
fileStream.CopyTo(innerStream);
//Create Acumatica file from .Zip file content
fileInfo = new PX.SM.FileInfo(info.Name, null, innerStream.ToArray());
//Saves file to Invoice
if (uploadFileMaint.SaveFile(fileInfo, FileExistsAction.CreateVersion))
{
PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, fileInfo);
}
}
}
}
}
}
}
}
}
return pxAdapter.Get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment