You are here

You are here

Creating a zip archive for download

This sample shows how to have SpringCM generate a zip archive and then download the file without storing the archive in a SpringCM folder.  The scmDestinationFolder and destinationName are passed as null.

Vertical Tabs

c#
//Here we are zipping a folder.  We could also give it a combo
//of a set of files and/or a set of folders
//Even if we don't send one of the object types, we must initialize it to an empty array
//In this example, we just zip a folder and then download the zip archive without having
//it stored in SpringCM
string folderToZipId = "<This the Id of the folder we want to zip retrieved from other API calls>";
string zipArchiveFileName = "<The name of the zip archive, should have a .zip extension>";
 
//The API recall requires folder objects, not Id's
var folderToZip = new SCMFolder { Id = folderToZipId };
 
//We will make the folder archive zip relative to the same folder we are zipping.  
//We could choose a folder higher up in the tree and the parent folders would be added to the archive
var folderToMakeZipArchiveRelativeTo = folderToZip;
 
//Create an SCMList of the documents and folders to be include in the zip
//In this case, just a single SpringCM folder's contents will be added
var nodesToZip = new SCMList { SCMDocuments = new SCMDocument[] { }, SCMFolders = new SCMFolder[] { folderToZip } };
 
//Initiate the request to create the zip archive and get the initial status
//Here we pass null for the target destination folder, since we are only downloading the file
var requestToken = springCMService.FolderArchive(token, folderToMakeZipArchiveRelativeTo, nodesToZip, null, zipArchiveFileName, null);
var result = springCMService.FolderArchiveGetStatus(token, requestToken);
 
//Keep checking every 10 seconds to see if zip is complete
while (result.Status != SCMQueueStatus.Success && result.Status != SCMQueueStatus.Failure)
{
	Thread.Sleep(10000);
	result = springCMService.FolderArchiveGetStatus(token, requestToken);
}
 
//See if we failed or had success in creating the zip archive
if (result.Status == SCMQueueStatus.Failure)
{
	Console.WriteLine("Folder Archive Failed - " + result.Message);
}
else
{
	//If we got this far, then we must have had success, download the file in chunks
	//Initialize the download variables 
	//position keeps track of where we are in downloading the file 
	//chunk size is how many bytes we ask SpringCM for at a time, in this 
	//case 1MB. 
	int position = 0;
	int chunkSize = 1048576;
	string localPathAndFileName = "<The path and file name for the local download>";
 
	//Create an in memory stream to write the downloaded document 
	using (FileStream stream = new FileStream(localPathAndFileName, FileMode.Create))
	{
		//The position increases until we have all the bytes 
		//so loop while there is still more to download 
		while (position < result.ArchiveSize)
		{
			//The final chunk will likely be smaller than the chunksize 
			//so we check if the next 1MB chunk would take us past the 
			//bytes left to be downloaded. If this is the case, 
			//we change the chunksize to the final set of the bytes 
			//We have to check everytime in this loop to see when we get 
			//to the end. 
			if (position + chunkSize >= result.ArchiveSize)
			{
				chunkSize = (int) (result.ArchiveSize - position);
			}
 
			//Get the current chunk, and then write it to the stream 
			byte[] currentChunk = springCMService.FolderArchiveDownload(token, requestToken, position,chunkSize);
			stream.Write(currentChunk, 0, chunkSize);
 
			//Move the position to be ready for the next chunk 
			position += chunkSize;
		}
	}
}
java
//Here we are zipping a folder.  We could also give it a combo
//of a set of files and/or a set of folders
//Even if we don't send one of the object types, we must initialize it to an empty array
//In this example, we just zip a folder and then download the zip archive without having
//it stored in SpringCM
String folderToZipId = "<This the Id of the folder we want to zip retrieved from other API calls>";
String zipArchiveFileName = "<The name of the zip archive, should have a .zip extension>";
 
//The API recall requires folder objects, not Id's
SCMFolder folderToZip = new SCMFolder();
folderToZip.setId(folderToZipId);
 
//We will make the folder archive zip relative to the same folder we are zipping.  
//We could choose a folder higher up in the tree and the parent folders would be added to the archive
SCMFolder folderToMakeZipArchiveRelativeTo = folderToZip;
 
//Create an SCMList of the documents and folders to be include in the zip
//In this case, just a single SpringCM folder's contents will be added
SCMList nodesToZip = new SCMList();
nodesToZip.setSCMDocuments(new SCMDocument[] { });
nodesToZip.setSCMFolders(new SCMFolder[]{folderToZip});
 
//Initiate the request to create the zip archive and get the initial status
//Here we pass null for the target destination folder, since we are only downloading the file
String requestToken = springCMService.folderArchive(token, folderToMakeZipArchiveRelativeTo, nodesToZip, null, zipArchiveFileName, null);
SCMFolderArchiveResult result = springCMService.folderArchiveGetStatus(token, requestToken);
 
//Keep checking every 10 seconds to see if zip is complete
while (result.getStatus() != SCMQueueStatus.Success && result.getStatus() != SCMQueueStatus.Failure)
{
	Thread.sleep(10000);
	result = springCMService.folderArchiveGetStatus(token, requestToken);
}
 
//See if we failed or had success in creating the zip archive
if (result.getStatus().equals(SCMQueueStatus.Failure))
{
	System.out.println("Folder Archive Failed - " + result.getMessage());
}
else
{
	//If we got this far, then we must have had success, download the file in chunks
	//Initialize the download variables 
	//position keeps track of where we are in downloading the file 
	//chunk size is how many bytes we ask SpringCM for at a time, in this 
	//case 1MB. 
	int position = 0;
	int chunkSize = 1048576;
	String localPathAndFileName = "<The path and file name for the local download>";
 
	//The position increases until we have all the bytes
	//so loop while there is still more to download
	ByteBuffer downloadedDocument = ByteBuffer.allocate((int)result.getArchiveSize());
 
	while (position < result.getArchiveSize())
	{
		//The final chunk will likely be smaller than the chunksize
		//so we check if the next 1MB chunk would take us past the
		//bytes left to be downloaded.  If this is the case,
		//we change the chunksize to the final set of the bytes
		//We have to check everytime in this loop to see when we get
		//to the end.
		if (position + chunkSize >= result.getArchiveSize())
		{
			chunkSize = (int)(result.getArchiveSize() - position);
		}
 
		//Get the current chunk, and then write it to the stream
		byte[] currentChunk = springCMService.folderArchiveDownload(token, requestToken, position,chunkSize);
		downloadedDocument.put(currentChunk, 0, chunkSize);
 
		//Move the position to be ready for the next chunk
		position += chunkSize;
 
	}
 
	FileOutputStream fileOutputStream = new FileOutputStream(localPathAndFileName);	   
	fileOutputStream.write(downloadedDocument.array());
	fileOutputStream.close();            
}