@@ -5,14 +5,24 @@ import com.simplemobiletools.commons.extensions.*
5
5
import net.lingala.zip4j.io.outputstream.ZipOutputStream
6
6
import net.lingala.zip4j.model.ZipParameters
7
7
import net.lingala.zip4j.model.enums.EncryptionMethod
8
+ import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile
9
+ import org.apache.commons.compress.archivers.tar.TarArchiveEntry
10
+ import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
11
+ import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream
12
+ import org.apache.commons.compress.utils.IOUtils
8
13
import java.io.Closeable
9
14
import java.io.File
15
+ import java.io.FileInputStream
16
+ import java.io.IOException
17
+ import java.nio.file.Files
18
+ import java.nio.file.Path
10
19
import java.util.LinkedList
11
20
21
+
12
22
class CompressionHelper {
13
23
14
24
companion object {
15
- fun compressPaths (
25
+ fun compress (
16
26
activity : BaseSimpleActivity ,
17
27
sourcePaths : List <String >,
18
28
targetPath : String ,
@@ -21,7 +31,7 @@ class CompressionHelper {
21
31
): Boolean {
22
32
return when (compressionFormat) {
23
33
CompressionFormat .ZIP -> compressToZip(activity, sourcePaths, targetPath, password)
24
- CompressionFormat .SEVEN_ZIP -> compressToSevenZip(activity, sourcePaths, targetPath, password )
34
+ CompressionFormat .SEVEN_ZIP -> compressToSevenZip(activity, sourcePaths, targetPath)
25
35
CompressionFormat .TAR_DEFLATE -> compressToTarDeflate(activity, sourcePaths, targetPath, password)
26
36
CompressionFormat .TAR_GZ -> compressToTarGz(activity, sourcePaths, targetPath, password)
27
37
CompressionFormat .TAR_SZ -> compressToTarSz(activity, sourcePaths, targetPath, password)
@@ -120,11 +130,34 @@ class CompressionHelper {
120
130
activity : BaseSimpleActivity ,
121
131
sourcePaths : List <String >,
122
132
targetPath : String ,
123
- password : String? = null
124
133
): Boolean {
125
- return false
134
+ try {
135
+ SevenZOutputFile (File (targetPath)).use { sevenZOutput ->
136
+ sourcePaths.forEach { sourcePath ->
137
+ Files .walk(File (sourcePath).toPath()).forEach { path ->
138
+ val file = path.toFile()
139
+
140
+ if (! activity.getIsPathDirectory(file.absolutePath)) {
141
+ FileInputStream (file).use { _ ->
142
+ val sevenZArchiveEntry = sevenZOutput.createArchiveEntry(file, file.toString())
143
+ sevenZOutput.putArchiveEntry(sevenZArchiveEntry)
144
+ sevenZOutput.write(Files .readAllBytes(file.toPath()))
145
+ sevenZOutput.closeArchiveEntry()
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ sevenZOutput.finish()
152
+ }
153
+ } catch (e: IOException ) {
154
+ e.printStackTrace()
155
+ return false
156
+ }
157
+ return true
126
158
}
127
159
160
+
128
161
private fun compressToTarDeflate (
129
162
activity : BaseSimpleActivity ,
130
163
sourcePaths : List <String >,
@@ -140,7 +173,27 @@ class CompressionHelper {
140
173
targetPath : String ,
141
174
password : String? = null
142
175
): Boolean {
143
- return false
176
+ val tarPath = targetPath.replace(CompressionFormat .TAR_GZ .extension, " .tar" )
177
+ val tarCreated = createTar(activity, sourcePaths, tarPath)
178
+
179
+ if (tarCreated) {
180
+ val tarFile = File (tarPath)
181
+ val fos = activity.getFileOutputStreamSync(targetPath, " application/gzip" )
182
+ try {
183
+ GzipCompressorOutputStream (fos).use { gzipOutput ->
184
+ gzipOutput.write(Files .readAllBytes(tarFile.toPath()))
185
+ gzipOutput.finish()
186
+ }
187
+ } catch (e: IOException ) {
188
+ e.printStackTrace()
189
+ return false
190
+ } finally {
191
+ tarFile.delete()
192
+ }
193
+ return true
194
+ } else {
195
+ return false
196
+ }
144
197
}
145
198
146
199
private fun compressToTarSz (
@@ -160,5 +213,32 @@ class CompressionHelper {
160
213
): Boolean {
161
214
return false
162
215
}
216
+
217
+ private fun createTar (activity : BaseSimpleActivity , sourcePaths : List <String >, targetPath : String ): Boolean {
218
+ val fos = activity.getFileOutputStreamSync(targetPath, " application/x-tar" )
219
+ try {
220
+ TarArchiveOutputStream (fos).use { archive ->
221
+ sourcePaths.forEach { sourcePath ->
222
+ Files .walk(File (sourcePath).toPath()).forEach { path: Path ->
223
+ val file = path.toFile()
224
+
225
+ if (! activity.getIsPathDirectory(file.absolutePath)) {
226
+ val tarArchiveEntry = TarArchiveEntry (file, file.toString())
227
+ FileInputStream (file).use { fis ->
228
+ archive.putArchiveEntry(tarArchiveEntry)
229
+ IOUtils .copy(fis, archive)
230
+ archive.closeArchiveEntry()
231
+ }
232
+ }
233
+ }
234
+ }
235
+ archive.finish()
236
+ }
237
+ } catch (e: IOException ) {
238
+ e.printStackTrace()
239
+ return false
240
+ }
241
+ return true
242
+ }
163
243
}
164
244
}
0 commit comments