-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExportZipArchive.ecl
216 lines (186 loc) · 7.09 KB
/
ExportZipArchive.ecl
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* A single macro that accepts a dataset and creates a .zip archive
* from the contents. This means you skip the following steps:
*
* 1) Write the data to Thor in the format you need (e.g.
* tab-delimited).
* 2) Despray the file to the landing zone.
* 3) Compress the landing zone file to a new archive.
*
* For large files, the time and space savings are considerable.
*
* To create something that looks like a .csv file, use:
* separator := ','
* quote := '"'
*
* The default values represent the task of "create a tab-delimited
* zip file of the data."
*
* See the end of this file for an example execution.
*
* @param outData The dataset to process; neither child
* datasets nor embedded records are supported;
* REQUIRED
* @param outPath A STRING representing the full path
* of the file to create; note that ".zip"
* will be appended to the path, and the
* file that will be extracted will be name
* given here; REQUIRED
* @param separator The field delimiter, as a STRING; OPTIONAL,
* defaults to a tab character
* @param quote The character used to quote string values that
* contain the SEPARATOR value; OPTIONAL,
* defaults to an empty string
* @param includeHeader If TRUE, the first line of the output will
* contain the name of the fields delimited by
* 'separator'; OPTIONAL, defaults to TRUE
*
* Origin: https://github.com/hpccsystems-solutions-lab/Useful_ECL
*/
EXPORT ExportZipArchive(outData,
outPath,
separator = '\'\t\'',
quote = '\'\'',
includeHeader = TRUE) := MACRO
IMPORT Std;
LOADXML('<xml/>');
#EXPORTXML(outDataFields, RECORDOF(outData));
#UNIQUENAME(needsDelim);
#SET(needsDelim, 0);
#UNIQUENAME(myOutPath);
LOCAL %myOutPath% := TRIM((STRING)outPath, LEFT, RIGHT);
#UNIQUENAME(outLayout);
LOCAL %outLayout% := {STRING rec};
#UNIQUENAME(AsQuoted);
LOCAL %AsQuoted%(STRING s) := IF(quote = '' OR separator = '' OR Std.Str.Find(s, separator, 1) = 0, s, quote + s + quote);
#SET(needsDelim, 0);
#UNIQUENAME(headerStr);
LOCAL %headerStr% := IF
(
includeHeader,
#FOR(outDataFields)
#FOR(Field)
#IF(%needsDelim% = 1) + separator + #END
%AsQuoted%(%'@name'%)
#SET(needsDelim, 1)
#END
#END
+ '\n',
''
);
#SET(needsDelim, 0);
#UNIQUENAME(rewrittenDataDS);
LOCAL %rewrittenDataDS% := PROJECT
(
outData,
TRANSFORM
(
%outLayout%,
SELF.rec := #FOR(outDataFields)
#FOR(Field)
#IF(%{@isRecord}% = 1 OR %{@isDataset}% = 1)
#ERROR('Only flat datasets are supported')
#END
#IF(%needsDelim% = 1) + separator + #END
%AsQuoted%(#EXPAND('(STRING)LEFT.' + %'@name'%))
#SET(needsDelim, 1)
#END
#END
+ '\n'
)
);
#UNIQUENAME(WriteFunc);
LOCAL %WriteFunc%(STREAMED DATASET(%outLayout%) ds, VARSTRING path, VARSTRING header) := EMBED(C++)
#option library archive
#include "archive.h"
#include "archive_entry.h"
#include <cstdio>
#include <string>
#body
std::string givenPath(path);
std::string destPath = givenPath + ".zip";
std::string destFilename(givenPath.substr(givenPath.find_last_of("/\\") + 1));
int errNum = 0;
// Try to remove any existing file
if (std::remove(destPath.c_str()) != 0)
{
if (errno != ENOENT)
{
rtlFail(errno, "While trying to delete preexisting archive file");
}
}
struct archive* archivePtr = archive_write_new();
archive_write_set_format_zip(archivePtr);
errNum = archive_write_open_filename(archivePtr, destPath.c_str());
if (errNum != ARCHIVE_OK)
{
rtlFail(archive_errno(archivePtr), archive_error_string(archivePtr));
}
struct archive_entry* entryPtr = archive_entry_new();
archive_entry_set_pathname(entryPtr, destFilename.c_str());
archive_entry_set_filetype(entryPtr, AE_IFREG);
archive_entry_set_perm(entryPtr, 0644);
archive_write_header(archivePtr, entryPtr);
if (header && header[0])
{
if (archive_write_data(archivePtr, header, strlen(header)) < 0)
{
rtlFail(archive_errno(archivePtr), archive_error_string(archivePtr));
}
}
while(true)
{
const byte* oneRow = static_cast<const byte*>(ds->nextRow());
if (!oneRow)
{
oneRow = static_cast<const byte*>(ds->nextRow());
if (!oneRow)
{
break;
}
}
const size32_t rowSize = *(reinterpret_cast<const size32_t*>(oneRow));
const char* rowBuffer = reinterpret_cast<const char*>(oneRow + sizeof(rowSize));
if (archive_write_data(archivePtr, rowBuffer, rowSize) < 0)
{
rtlFail(archive_errno(archivePtr), archive_error_string(archivePtr));
}
rtlReleaseRow(oneRow);
}
errNum = archive_write_finish_entry(archivePtr);
if (errNum != ARCHIVE_OK)
{
rtlFail(archive_errno(archivePtr), archive_error_string(archivePtr));
}
archive_entry_free(entryPtr);
errNum = archive_write_close(archivePtr);
if (errNum != ARCHIVE_OK)
{
rtlFail(archive_errno(archivePtr), archive_error_string(archivePtr));
}
archive_write_free(archivePtr);
ENDEMBED;
%WriteFunc%(%rewrittenDataDS%, %myOutPath%, %headerStr%);
ENDMACRO;
/*=====================================================================================
// Example call
ds := DATASET
(
10000,
TRANSFORM
(
{
UNSIGNED4 id,
UNSIGNED4 n
},
SELF.id := COUNTER,
SELF.n := RANDOM()
),
DISTRIBUTED
);
LZ_DIR := '/var/lib/HPCCSystems/mydropzone';
DEST_FILE := LZ_DIR + '/test.txt';
// Archive will be named 'test.txt.zip'; the uncompressed file will
// be named 'test.txt'.
ExportZipArchive(ds, DEST_FILE);
*/