Home Blog Zipped Up Acumatica – Manipulating Files Part 1 .Zip

Zipped Up Acumatica – Manipulating Files Part 1 .Zip

We review how you can archive files attached to a document within an Acumatica instance or the attachment of files from within a .Zip file to a document within an Acumatica instance.
Joshua van Hoesen | March 29, 2022

Zipped Up Acumatica – Manipulating Files Part 1 .Zip

Introduction

In our years ( ASI LLC ) developing solutions within the Acumatica framework we have experienced multiple instances in which files of one type or another need to be generated and manipulated within the system.

An example usage which we will review in this article is the archiving of files attached to a document within an Acumatica instance -or- the attachment of files from within a .Zip file to a document within an Acumatica instance.

.Zip – PX.Common.Std

To utilize .ZIP files within Acumatica you will need to add reference to the PX.Common.Std .dll within your project. This .dll can be found in the bin folder of the instance install.

ZipArchive

CreateFrom

This static method returns an instance of ZipArchive which represents a .ZIP file. This method will take Stream os and bool onlyRead as parameters. 

The code example below illustrates the creation of a new zip file utilizing a new MemoryStream and setting the onlyRead argument as false to allow the addition of files to our created .Zip file.

GIST: [Creation of .Zip file (github.com)]

CreateFrom is also utilized when opening a pre-existing .ZIP file. To open a pre-existing file you must first retrieve its byte[] and create a new MemoryStream from the data, setting the bool onlyRead parameter as true. 

GIST: [Open existing .Zip file (github.com)]

AddFile

This public void method adds a file from the given name and a byte[] array of the file to be included in the .Zip file.

GIST: [Addition of file to created .Zip archive (github.com)]

GetFiles

This method returns a list of files within the .Zip archive; each file’s information is encapsulated within the inner ItemInfo class. 

GIST: [Iterate through file names in Archive (github.com)]

GetItems

This method is utilized when you only wish to pull the files from a specific directory within the archive. It utilizes the string parameter directory.

OpenRead

This method returns a Stream from a file within the archive. The file is specified by providing a file name, this name 

GIST: [Retrieving a file from ZipArchive (github.com)]

Examples

Below you will find examples of creating a new .Zip archive within Acumatica as well as taking a .Zip file attached to a document and reading / saving the contents within.

Archive Creation

The following example will retrieve the list of files attached to an Acumatica document and create a .Zip archive which is then attached.

GIST: [Opens a zip file attached to an Acumatica document (github.com)]

Archive Retrieval

The following example will retrieve the .Zip file attached to an Acumatica document and attach all documents within that archive to the source document.

GIST: [Button to create .Zip file from files attached to Acumatica document (github.com)]

Conclusion

With the usage of Libraries provided within an Acumatica ERP install, we are able to create and manipulate .Zip files

Happy Coding!

Blog Author

Lead Software Engineer at Accounting System Integrators. He leads the development team, which built NonProfitPlus – business management software engineered with non-profit organizations in mind and powered by Acumatica ERP to deliver a suite of fully integrated applications.

Receive blog updates in your Inbox.