Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mete-data-compressor
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fengyuncheng
mete-data-compressor
Commits
20a5d840
Commit
20a5d840
authored
Oct 18, 2022
by
fengyuncheng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
v1
parent
c2f3d5da
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
16 deletions
+73
-16
.gitignore
.gitignore
+2
-1
Main.java
src/main/java/com/hgny/protobuf/Main.java
+21
-10
Tools.java
src/main/java/com/hgny/protobuf/utils/Tools.java
+50
-5
No files found.
.gitignore
View file @
20a5d840
...
@@ -10,3 +10,4 @@ save3
...
@@ -10,3 +10,4 @@ save3
/output/
/output/
/data/
/data/
/.idea/
/.idea/
*.txt
\ No newline at end of file
src/main/java/com/hgny/protobuf/Main.java
View file @
20a5d840
...
@@ -27,7 +27,7 @@ import java.util.stream.Stream;
...
@@ -27,7 +27,7 @@ import java.util.stream.Stream;
public
class
Main
{
public
class
Main
{
private
static
List
<
String
>
SKIP_KEY
=
new
ArrayList
<>(
List
.
of
(
new
String
[]{
"mdp"
,
"mdptime"
,
"mdkwh"
,
"mdkwhtime"
}));
private
static
List
<
String
>
SKIP_KEY
=
new
ArrayList
<>(
List
.
of
(
new
String
[]{
"mdp"
,
"mdptime"
,
"mdkwh"
,
"mdkwhtime"
}));
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InvocationTargetException
,
IllegalAccessException
{
public
static
void
main
(
String
[]
args
)
throws
IOException
,
InvocationTargetException
,
IllegalAccessException
,
InterruptedException
{
// test2();
// test2();
// test5();
// test5();
// test6();
// test6();
...
@@ -41,17 +41,26 @@ public class Main {
...
@@ -41,17 +41,26 @@ public class Main {
if
(
gz
.
isDirectory
())
{
if
(
gz
.
isDirectory
())
{
continue
;
continue
;
}
}
Tools
.
extraTarGz
(
gz
,
"./data"
);
String
gzName
=
gz
.
getName
();
}
System
.
out
.
println
(
"parse: "
+
gzName
);
String
folderName
=
Tools
.
extraTarGz
(
gz
,
"./data"
);
// 遍历目录解压tar.bz2
// 遍历目录解压tar.bz2
for
(
File
file
:
new
File
(
"./data"
).
listFiles
())
{
for
(
File
file
:
new
File
(
"./data/"
+
folderName
).
listFiles
())
{
if
(!
file
.
isDirectory
())
{
if
(!
file
.
isDirectory
())
{
continue
;
continue
;
}
}
String
name
=
file
.
getName
();
String
name
=
file
.
getName
();
scanDir
(
file
,
"./data/"
+
name
,
0
);
scanDir
(
file
,
"./data/"
+
folderName
+
"/"
+
name
,
1
);
}
}
// 处理完成,删除解压出来的文件夹
int
index
=
gzName
.
indexOf
(
"."
);
String
fName
=
folderName
.
substring
(
0
,
index
);
deleteFolder
(
new
File
(
"./data/"
+
fName
));
Tools
.
compressTarGz
(
"./output/"
+
fName
,
new
File
(
"./output/"
+
fName
+
".tar.gz"
));
deleteFolder
(
new
File
(
"./output/"
+
fName
));
System
.
out
.
println
(
"spend: "
+
(
System
.
currentTimeMillis
()
-
start
)
+
"ms"
);
System
.
out
.
println
(
"spend: "
+
(
System
.
currentTimeMillis
()
-
start
)
+
"ms"
);
start
=
System
.
currentTimeMillis
();
}
// 压缩
// 压缩
// for (File file : new File("./data").listFiles()) {
// for (File file : new File("./data").listFiles()) {
// if (!file.isDirectory()) {
// if (!file.isDirectory()) {
...
@@ -190,6 +199,8 @@ public class Main {
...
@@ -190,6 +199,8 @@ public class Main {
}
}
private
static
void
deleteFolder
(
File
file
)
{
private
static
void
deleteFolder
(
File
file
)
{
// 释放stream占用的文件
System
.
gc
();
for
(
File
f
:
file
.
listFiles
())
{
for
(
File
f
:
file
.
listFiles
())
{
if
(
f
.
isDirectory
())
{
if
(
f
.
isDirectory
())
{
deleteFolder
(
f
);
deleteFolder
(
f
);
...
...
src/main/java/com/hgny/protobuf/utils/Tools.java
View file @
20a5d840
...
@@ -4,10 +4,15 @@ import com.google.gson.Gson;
...
@@ -4,10 +4,15 @@ import com.google.gson.Gson;
import
org.apache.commons.compress.archivers.ArchiveEntry
;
import
org.apache.commons.compress.archivers.ArchiveEntry
;
import
org.apache.commons.compress.archivers.tar.TarArchiveEntry
;
import
org.apache.commons.compress.archivers.tar.TarArchiveEntry
;
import
org.apache.commons.compress.archivers.tar.TarArchiveInputStream
;
import
org.apache.commons.compress.archivers.tar.TarArchiveInputStream
;
import
org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
;
import
org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
;
import
org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
;
import
org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
;
import
org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream
;
import
org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream
;
import
org.apache.commons.compress.utils.IOUtils
;
import
java.io.*
;
import
java.io.*
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.charset.StandardCharsets
;
import
java.time.LocalDateTime
;
import
java.time.LocalDateTime
;
import
java.time.ZoneOffset
;
import
java.time.ZoneOffset
;
...
@@ -18,6 +23,7 @@ import java.util.List;
...
@@ -18,6 +23,7 @@ import java.util.List;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
import
java.util.zip.GZIPInputStream
;
import
java.util.zip.GZIPInputStream
;
import
java.util.zip.GZIPOutputStream
;
public
class
Tools
{
public
class
Tools
{
...
@@ -26,16 +32,16 @@ public class Tools {
...
@@ -26,16 +32,16 @@ public class Tools {
return
Integer
.
parseInt
(
t
.
substring
(
0
,
t
.
length
()
-
3
));
return
Integer
.
parseInt
(
t
.
substring
(
0
,
t
.
length
()
-
3
));
}
}
public
static
void
extraTarGz
(
File
file
,
String
path
)
{
public
static
String
extraTarGz
(
File
file
,
String
path
)
{
if
(!
file
.
getName
().
contains
(
".tar.gz"
))
{
if
(!
file
.
getName
().
contains
(
".tar.gz"
))
{
return
;
return
null
;
}
}
try
(
TarArchiveInputStream
inputStream
=
new
TarArchiveInputStream
(
try
(
TarArchiveInputStream
inputStream
=
new
TarArchiveInputStream
(
new
GZIPInputStream
(
new
GZIPInputStream
(
new
FileInputStream
(
file
)
new
FileInputStream
(
file
)
)
)
))
{
))
{
extraTar
(
inputStream
,
path
);
return
extraTar
(
inputStream
,
path
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
...
@@ -70,7 +76,6 @@ public class Tools {
...
@@ -70,7 +76,6 @@ public class Tools {
createDir
(
path
,
entryName
);
createDir
(
path
,
entryName
);
}
else
{
}
else
{
createDir
(
tmpFile
.
getParent
()
+
"/"
,
null
);
createDir
(
tmpFile
.
getParent
()
+
"/"
,
null
);
System
.
out
.
println
(
"extra: "
+
tmpFile
.
getPath
());
try
(
FileOutputStream
outputStream
=
new
FileOutputStream
(
tmpFile
))
{
try
(
FileOutputStream
outputStream
=
new
FileOutputStream
(
tmpFile
))
{
int
read
=
0
;
int
read
=
0
;
byte
[]
buffer
=
new
byte
[
1024
];
byte
[]
buffer
=
new
byte
[
1024
];
...
@@ -120,13 +125,53 @@ public class Tools {
...
@@ -120,13 +125,53 @@ public class Tools {
try
(
FileOutputStream
outputStream
=
new
FileOutputStream
(
file
);
try
(
FileOutputStream
outputStream
=
new
FileOutputStream
(
file
);
BZip2CompressorOutputStream
zip
=
new
BZip2CompressorOutputStream
(
outputStream
,
9
)
BZip2CompressorOutputStream
zip
=
new
BZip2CompressorOutputStream
(
outputStream
,
9
)
)
{
)
{
System
.
out
.
println
(
"compress: "
+
file
.
getPath
());
zip
.
write
(
data
.
getBytes
(
StandardCharsets
.
UTF_8
));
zip
.
write
(
data
.
getBytes
(
StandardCharsets
.
UTF_8
));
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
throw
new
RuntimeException
(
e
);
}
}
}
}
public
static
void
compressTarGz
(
String
path
,
File
file
)
throws
IOException
{
createTarGZ
(
path
,
file
);
}
public
static
void
createTarGZ
(
String
path
,
File
file
)
throws
IOException
{
try
(
FileOutputStream
fOut
=
new
FileOutputStream
(
file
);
BufferedOutputStream
bOut
=
new
BufferedOutputStream
(
fOut
);
GzipCompressorOutputStream
gzOut
=
new
GzipCompressorOutputStream
(
bOut
);
TarArchiveOutputStream
tOut
=
new
TarArchiveOutputStream
(
gzOut
)
)
{
addFileToTarGz
(
tOut
,
path
,
""
);
tOut
.
flush
();
}
}
private
static
void
addFileToTarGz
(
TarArchiveOutputStream
tOut
,
String
path
,
String
base
)
throws
IOException
{
File
f
=
new
File
(
path
);
String
entryName
=
base
+
f
.
getName
();
TarArchiveEntry
tarEntry
=
new
TarArchiveEntry
(
f
,
entryName
);
tOut
.
putArchiveEntry
(
tarEntry
);
if
(
f
.
isFile
())
{
IOUtils
.
copy
(
new
FileInputStream
(
f
),
tOut
);
tOut
.
flush
();
tOut
.
closeArchiveEntry
();
}
else
{
tOut
.
closeArchiveEntry
();
File
[]
children
=
f
.
listFiles
();
if
(
children
!=
null
)
{
for
(
File
child
:
children
)
{
addFileToTarGz
(
tOut
,
child
.
getAbsolutePath
(),
entryName
+
"/"
);
}
}
}
}
public
static
void
createDir
(
String
path
,
String
subPath
)
{
public
static
void
createDir
(
String
path
,
String
subPath
)
{
File
file
=
new
File
(
path
);
File
file
=
new
File
(
path
);
if
(!(
subPath
==
null
||
subPath
.
trim
().
equals
(
""
)))
{
if
(!(
subPath
==
null
||
subPath
.
trim
().
equals
(
""
)))
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment