Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
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
曲欣亮
common
Commits
bb791662
Commit
bb791662
authored
Oct 14, 2020
by
张永
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add 删除指定后缀文件
parent
637e5fec
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
156 additions
and
145 deletions
+156
-145
FileUtil.java
src/main/java/com/egolm/common/FileUtil.java
+10
-0
ZipUtil.java
src/main/java/com/egolm/common/ZipUtil.java
+146
-145
No files found.
src/main/java/com/egolm/common/FileUtil.java
View file @
bb791662
...
@@ -377,6 +377,16 @@ public class FileUtil {
...
@@ -377,6 +377,16 @@ public class FileUtil {
}
}
}
}
}
}
public
static
void
fileDeleteBySuffix
(
String
filePath
,
String
suffix
)
{
File
file
=
new
File
(
filePath
);
File
[]
files
=
file
.
listFiles
();
for
(
File
f:
files
)
{
if
(
f
.
getName
().
endsWith
(
suffix
))
{
f
.
delete
();
}
}
}
public
static
byte
[]
streamToBytes
(
InputStream
instream
)
{
public
static
byte
[]
streamToBytes
(
InputStream
instream
)
{
byte
[]
bytes
=
null
;
byte
[]
bytes
=
null
;
...
...
src/main/java/com/egolm/common/ZipUtil.java
View file @
bb791662
package
com
.
egolm
.
common
;
package
com
.
egolm
.
common
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipFile
;
import
java.util.zip.ZipFile
;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipInputStream
;
import
java.util.zip.ZipOutputStream
;
import
java.util.zip.ZipOutputStream
;
import
com.egolm.common.exception.ZipUtilException
;
import
com.egolm.common.exception.ZipUtilException
;
public
final
class
ZipUtil
{
public
final
class
ZipUtil
{
public
static
File
zip
(
String
path
)
{
public
static
File
zip
(
String
path
)
{
return
zip
(
new
File
(
path
));
return
zip
(
new
File
(
path
));
}
}
public
static
File
zip
(
File
file
)
{
public
static
File
zip
(
File
file
)
{
return
zip
(
file
,
file
.
getAbsolutePath
()
+
".zip"
);
return
zip
(
file
,
file
.
getAbsolutePath
()
+
".zip"
);
}
}
public
static
File
zip
(
String
path
,
String
zipTo
)
{
public
static
File
zip
(
String
path
,
String
zipTo
)
{
return
zip
(
new
File
(
path
),
zipTo
);
return
zip
(
new
File
(
path
),
zipTo
);
}
}
public
static
File
zip
(
File
file
,
String
zipTo
)
{
public
static
File
zip
(
File
file
,
String
zipTo
)
{
File
target
=
null
;
File
target
=
null
;
if
(
file
.
exists
())
{
if
(
file
.
exists
())
{
target
=
FileUtil
.
createFile
(
zipTo
);
target
=
FileUtil
.
createFile
(
zipTo
);
FileOutputStream
fos
=
null
;
FileOutputStream
fos
=
null
;
ZipOutputStream
zos
=
null
;
ZipOutputStream
zos
=
null
;
try
{
try
{
fos
=
new
FileOutputStream
(
target
);
fos
=
new
FileOutputStream
(
target
);
zos
=
new
ZipOutputStream
(
new
BufferedOutputStream
(
fos
));
zos
=
new
ZipOutputStream
(
new
BufferedOutputStream
(
fos
));
addEntry
(
null
,
file
,
zos
);
addEntry
(
null
,
file
,
zos
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
finally
{
}
finally
{
try
{
try
{
if
(
zos
!=
null
)
{
if
(
zos
!=
null
)
{
zos
.
close
();
zos
.
close
();
}
}
if
(
fos
!=
null
)
{
if
(
fos
!=
null
)
{
fos
.
close
();
fos
.
close
();
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
}
}
}
}
}
return
target
;
return
target
;
}
}
private
static
void
addEntry
(
String
base
,
File
source
,
ZipOutputStream
zos
)
{
private
static
void
addEntry
(
String
base
,
File
source
,
ZipOutputStream
zos
)
{
String
entry
=
null
;
String
entry
=
null
;
if
(
StringUtil
.
isBlank
(
base
))
{
if
(
StringUtil
.
isBlank
(
base
))
{
entry
=
source
.
getName
();
entry
=
source
.
getName
();
}
else
{
}
else
{
entry
=
base
+
File
.
separator
+
source
.
getName
();
entry
=
base
+
File
.
separator
+
source
.
getName
();
}
}
if
(
source
.
isDirectory
())
{
if
(
source
.
isDirectory
())
{
for
(
File
file
:
source
.
listFiles
())
{
for
(
File
file
:
source
.
listFiles
())
{
addEntry
(
entry
,
file
,
zos
);
addEntry
(
entry
,
file
,
zos
);
}
}
}
else
{
}
else
{
FileInputStream
fis
=
null
;
FileInputStream
fis
=
null
;
BufferedInputStream
bis
=
null
;
BufferedInputStream
bis
=
null
;
try
{
try
{
try
{
try
{
byte
[]
buffer
=
new
byte
[
1024
*
10
];
byte
[]
buffer
=
new
byte
[
1024
*
10
];
fis
=
new
FileInputStream
(
source
);
fis
=
new
FileInputStream
(
source
);
bis
=
new
BufferedInputStream
(
fis
,
buffer
.
length
);
bis
=
new
BufferedInputStream
(
fis
,
buffer
.
length
);
int
read
=
0
;
int
read
=
0
;
zos
.
putNextEntry
(
new
ZipEntry
(
entry
));
zos
.
putNextEntry
(
new
ZipEntry
(
entry
));
while
((
read
=
bis
.
read
(
buffer
,
0
,
buffer
.
length
))
!=
-
1
)
{
while
((
read
=
bis
.
read
(
buffer
,
0
,
buffer
.
length
))
!=
-
1
)
{
zos
.
write
(
buffer
,
0
,
read
);
zos
.
write
(
buffer
,
0
,
read
);
}
}
zos
.
closeEntry
();
zos
.
closeEntry
();
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
finally
{
}
finally
{
if
(
bis
!=
null
)
{
if
(
bis
!=
null
)
{
bis
.
close
();
bis
.
close
();
}
}
if
(
fis
!=
null
)
{
if
(
fis
!=
null
)
{
fis
.
close
();
fis
.
close
();
}
}
}
}
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
}
}
}
}
}
public
static
void
unzip
(
String
zipName
)
{
public
static
void
unzip
(
String
zipName
)
{
File
source
=
new
File
(
zipName
);
File
source
=
new
File
(
zipName
);
unzip
(
source
,
source
.
getParentFile
().
getAbsolutePath
()
+
source
.
getName
());
unzip
(
source
,
source
.
getParentFile
().
getAbsolutePath
()
+
source
.
getName
());
}
}
public
static
void
unzip
(
File
source
)
{
public
static
void
unzip
(
File
source
)
{
unzip
(
source
,
source
.
getParentFile
().
getAbsolutePath
()
+
source
.
getName
());
unzip
(
source
,
source
.
getParentFile
().
getAbsolutePath
()
+
source
.
getName
());
}
}
public
static
void
unzip
(
String
zipName
,
String
targetPath
)
{
public
static
void
unzip
(
String
zipName
,
String
targetPath
)
{
unzip
(
new
File
(
zipName
),
targetPath
);
unzip
(
new
File
(
zipName
),
targetPath
);
}
}
public
static
void
unzip
(
File
file
,
String
descDir
)
{
public
static
void
unzip
(
File
file
,
String
descDir
)
{
ZipFile
zipFile
=
null
;
ZipFile
zipFile
=
null
;
ZipInputStream
zis
=
null
;
ZipInputStream
zis
=
null
;
try
{
try
{
zipFile
=
new
ZipFile
(
file
);
zipFile
=
new
ZipFile
(
file
);
zis
=
new
ZipInputStream
(
new
FileInputStream
(
file
));
zis
=
new
ZipInputStream
(
new
FileInputStream
(
file
));
ZipEntry
entry
=
null
;
ZipEntry
entry
=
null
;
while
((
entry
=
zis
.
getNextEntry
())
!=
null
&&
!
entry
.
isDirectory
())
{
while
((
entry
=
zis
.
getNextEntry
())
!=
null
&&
!
entry
.
isDirectory
())
{
InputStream
is
=
zipFile
.
getInputStream
(
entry
);
InputStream
is
=
zipFile
.
getInputStream
(
entry
);
FileUtil
.
streamToFile
(
is
,
descDir
+
File
.
separator
+
entry
.
getName
());
FileUtil
.
streamToFile
(
is
,
descDir
+
File
.
separator
+
entry
.
getName
());
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
finally
{
}
finally
{
try
{
try
{
if
(
zipFile
!=
null
)
{
if
(
zipFile
!=
null
)
{
zipFile
.
close
();
zipFile
.
close
();
}
}
if
(
zis
!=
null
)
{
if
(
zis
!=
null
)
{
zis
.
close
();
zis
.
close
();
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
ZipUtilException
(
e
);
throw
new
ZipUtilException
(
e
);
}
}
}
}
}
}
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
String
targetPath
=
"D:\\Green\\IDC\\50010"
;
String
targetPath
=
"D:\\data\\erp\\91310114594794273Y"
;
File
file
=
ZipUtil
.
zip
(
targetPath
);
File
file
=
ZipUtil
.
zip
(
targetPath
);
ZipUtil
.
unzip
(
file
,
"D:\\IDC"
);
System
.
out
.
println
(
file
.
getName
());
}
//ZipUtil.unzip(file, "D:\\data\\erp\\91310114594794273Y");
}
}
}
\ No newline at end of file
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