-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipDocuments.java
More file actions
132 lines (107 loc) · 4.27 KB
/
ZipDocuments.java
File metadata and controls
132 lines (107 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.
package ziphandling.actions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.mendix.core.Core;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
/**
* This Java action will zip all files given in the list of documets in one zip file output.zip.
*/
public class ZipDocuments extends CustomJavaAction<IMendixObject>
{
/** @deprecated use com.mendix.utils.ListUtils.map(ListOfDocument, com.mendix.systemwideinterfaces.core.IEntityProxy::getMendixObject) instead. */
@java.lang.Deprecated(forRemoval = true)
private final java.util.List<IMendixObject> __ListOfDocument;
private final java.util.List<system.proxies.FileDocument> ListOfDocument;
/** @deprecated use ZipFile.getMendixObject() instead. */
@java.lang.Deprecated(forRemoval = true)
private final IMendixObject __ZipFile;
private final system.proxies.FileDocument ZipFile;
public ZipDocuments(
IContext context,
java.util.List<IMendixObject> _listOfDocument,
IMendixObject _zipFile
)
{
super(context);
this.__ListOfDocument = _listOfDocument;
this.ListOfDocument = java.util.Optional.ofNullable(_listOfDocument)
.orElse(java.util.Collections.emptyList())
.stream()
.map(listOfDocumentElement -> system.proxies.FileDocument.initialize(getContext(), listOfDocumentElement))
.collect(java.util.stream.Collectors.toList());
this.__ZipFile = _zipFile;
this.ZipFile = _zipFile == null ? null : system.proxies.FileDocument.initialize(getContext(), _zipFile);
}
@java.lang.Override
public IMendixObject executeAction() throws Exception
{
// BEGIN USER CODE
File tempZipFile = File.createTempFile("zipfile", "temp");
ZipOutputStream zipStream = new ZipOutputStream(new FileOutputStream(tempZipFile));
List<String> fileList = new ArrayList<String>();
for (system.proxies.FileDocument file : ListOfDocument) {
String fileName = file.getName();
// skip files with no names
if (fileName != null) {
//skip files with no contents
if (file.getHasContents()) {
if (fileList.contains(fileName)) fileName = file.getMendixObject().getId().toLong() + " " + fileName;
InputStream inputStream = Core.getFileDocumentContent(getContext(), file.getMendixObject());
ZipDocuments.addToZipFile(inputStream,fileName,zipStream);
zipStream.closeEntry();
fileList.add(fileName);
}
}
}
zipStream.close();
IMendixObject NewZipFile = Core.instantiate(getContext(), "System.FileDocument");
NewZipFile.setValue(getContext(), "Name" , "output.zip" );
NewZipFile.setValue(getContext(), "DeleteAfterDownload" , "true" );
InputStream zipInputStream = new FileInputStream(tempZipFile);
Core.storeFileDocumentContent(getContext(), NewZipFile, zipInputStream);
zipInputStream.close();
tempZipFile.delete();
return NewZipFile;
// END USER CODE
}
/**
* Returns a string representation of this action
* @return a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "ZipDocuments";
}
// BEGIN EXTRA CODE
public static void addToZipFile(InputStream fis,String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
ZipEntry zipEntry = new ZipEntry(fileName);
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
// END EXTRA CODE
}