Skip to content

Instantly share code, notes, and snippets.

View jvanhoesen's full-sized avatar

Joshua Van Hoesen jvanhoesen

View GitHub Profile
@jvanhoesen
jvanhoesen / ZipArchiveCreateBtn.cs
Created March 28, 2022 20:22
Button to create .Zip file from files attached to Acumatica document
public PXAction<ARInvoice> CreateZip;
[PXButton(CommitChanges = false)]
[PXUIField(DisplayName = "Create .Zip", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Update, Enabled = true)]
protected virtual IEnumerable createZip(PXAdapter pxAdapter)
{
if (Base.Document.Current != null)
{
UploadFileMaintenance uploadFileMaint = PXGraph.CreateInstance<UploadFileMaintenance>();
@jvanhoesen
jvanhoesen / ZipArchiveOpenBtn.cs
Created March 28, 2022 19:49
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();
@jvanhoesen
jvanhoesen / ZipArchiveRetrieve.cs
Created March 28, 2022 18:49
Retrieving a file from ZipArchive
//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());
@jvanhoesen
jvanhoesen / ZipArchiveIterate.cs
Created March 28, 2022 18:48
Iterate through file names in Archive
//Iterate through all files and retrieve each files ItemInfo containing it's name
foreach (ZipArchive.ItemInfo info in zip.GetFiles())
{
//Use file name to retrieve file from .Zip Archive
}
@jvanhoesen
jvanhoesen / ZipArchiveOpen.cs
Created March 28, 2022 18:36
Open existing .Zip file
//Retrieving .ZIP file attached to Acumatica document
PX.SM.FileInfo fileInfo = uploadFileMaint.GetFile(zipFile);
if(fileInfo != null)
{
//Create a new memory stream from .ZIP file data
using (MemoryStream stream = new MemoryStream(fileInfo.BinData))
{
//Create new instance of ZipArchive to interact with .ZIP file
using (ZipArchive zip = ZipArchive.CreateFrom(stream, true))
{
@jvanhoesen
jvanhoesen / FileAddition.cs
Created February 2, 2022 21:18
Addition of file to created .Zip archive
using (ZipArchive zip = ZipArchive.CreateFrom(zipStream, false))
{
using (MemoryStream fileStream = new MemoryStream())
{
fileStream.Seek(0, SeekOrigin.Begin);
file.Write(fileStream);
fileStream.Seek(0, SeekOrigin.Begin);
@jvanhoesen
jvanhoesen / ZipArchiveCreate.cs
Created February 2, 2022 21:12
Creation of .Zip file
using (MemoryStream zipStream = new MemoryStream())
{
zipStream.Seek(0, SeekOrigin.Begin);
//2nd Parameter determines if .Zip is read only
using (ZipArchive zip = ZipArchive.CreateFrom(zipStream, false))
{
//Read or write data
}
}
@jvanhoesen
jvanhoesen / Prefetch.cs
Created June 2, 2021 18:50
Prefetch default implementation vs advanced implementation
//Default implementation
public void Prefetch()
{
_InventoryItems.Clear();
foreach (InventoryItem item in PXDatabase.SelectRecords<InventoryItem>())
{
_InventoryItems[item.InventoryID.Value] = item;
}
}
@jvanhoesen
jvanhoesen / AAInventoryItemCache.cs
Created June 2, 2021 15:25
Updated IPrefetchable implementation utilizing AACache
internal sealed class AAInventoryItemCache : IPrefetchable
{
private Dictionary<int, InventoryItem> _InventoryItems = new Dictionary<int, InventoryItem>();
public void Prefetch()
{
_InventoryItems.Clear();
foreach (InventoryItem item in AACache<InventoryItem>.SelectRecords())
{
@jvanhoesen
jvanhoesen / Properties.cs
Last active June 2, 2021 17:49
Property declaration of AACache
private static CacheStaticInfo _staticInfo;
private static Type _pXCacheExtensionCollectionType;
protected static CacheStaticInfo StaticInfo
{
get
{
if (_staticInfo == null)
{