Skip to content

Commit 41d6959

Browse files
Refactoring
1 parent 8633401 commit 41d6959

File tree

56 files changed

+632
-648
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+632
-648
lines changed

src/main/java/ru/olegcherednik/zip4jvm/UnzipIt.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import lombok.AccessLevel;
2626
import lombok.RequiredArgsConstructor;
2727

28-
import java.io.IOException;
2928
import java.io.InputStream;
3029
import java.nio.file.Path;
3130
import java.util.Collection;
@@ -108,10 +107,9 @@ public UnzipIt password(char[] password) {
108107
/**
109108
* Extract all existed in {@link #zip} archive entries into {@link #dstDir} using {@link #settings}.
110109
*
111-
* @throws IOException in case of any problem with file access
112110
* @throws IncorrectPasswordException in case of password incorrect
113111
*/
114-
public void extract() throws IOException, IncorrectPasswordException {
112+
public void extract() throws IncorrectPasswordException {
115113
open().extract(dstDir);
116114
}
117115

@@ -123,10 +121,9 @@ public void extract() throws IOException, IncorrectPasswordException {
123121
* keeping the initial structure.
124122
*
125123
* @param fileName not blank file name
126-
* @throws IOException in case of any problem with file access
127124
* @throws IncorrectPasswordException in case of password incorrect
128125
*/
129-
public void extract(String fileName) throws IOException, IncorrectPasswordException {
126+
public void extract(String fileName) throws IncorrectPasswordException {
130127
requireNotBlank(fileName, "UnzipIt.fileName");
131128
open().extract(dstDir, fileName);
132129
}
@@ -140,10 +137,9 @@ public void extract(String fileName) throws IOException, IncorrectPasswordExcept
140137
* keeping the initial structure.
141138
*
142139
* @param fileNames not {@literal null} file names
143-
* @throws IOException in case of any problem with file access
144140
* @throws IncorrectPasswordException in case of password incorrect
145141
*/
146-
public void extract(Collection<String> fileNames) throws IOException {
142+
public void extract(Collection<String> fileNames) throws IncorrectPasswordException {
147143
requireNotNull(fileNames, "UnzipIt.fileNames");
148144
open().extract(dstDir, fileNames);
149145
}
@@ -154,10 +150,9 @@ public void extract(Collection<String> fileNames) throws IOException {
154150
*
155151
* @param fileName not blank file name
156152
* @return not {@literal null} {@link InputStream} instance; for directory entry retrieve empty {@link InputStream}
157-
* @throws IOException in case of any problem with file access
158153
* @throws IncorrectPasswordException in case of password incorrect
159154
*/
160-
public InputStream stream(String fileName) throws IOException {
155+
public InputStream stream(String fileName) throws IncorrectPasswordException {
161156
requireNotBlank(fileName, "UnzipIt.fileName");
162157
return ZipFile.reader(srcZip, settings).extract(fileName).getInputStream();
163158
}
@@ -167,10 +162,9 @@ public InputStream stream(String fileName) throws IOException {
167162
* archive.
168163
*
169164
* @return not {@literal null} instance of {@link ZipFile.Reader}
170-
* @throws IOException in case of any problem with file access
171165
*/
172166
// @NotNull
173-
public ZipFile.Reader open() throws IOException {
167+
public ZipFile.Reader open() {
174168
return ZipFile.reader(srcZip, settings);
175169
}
176170

src/main/java/ru/olegcherednik/zip4jvm/ZipFile.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@
5353
@NoArgsConstructor(access = AccessLevel.PRIVATE)
5454
public final class ZipFile {
5555

56-
static Writer writer(Path zip, ZipSettings settings) throws IOException {
56+
static Writer writer(Path zip, ZipSettings settings) {
5757
return new ZipEngine(zip, settings);
5858
}
5959

6060
static Reader reader(SrcZip srcZip, UnzipSettings settings) {
6161
return new UnzipEngine(srcZip, settings);
6262
}
6363

64-
static Info info(SrcZip srcZip, ZipInfoSettings settings) throws IOException {
64+
static Info info(SrcZip srcZip, ZipInfoSettings settings) {
6565
return new InfoEngine(srcZip, settings);
6666
}
6767

@@ -144,13 +144,13 @@ public interface Writer extends Closeable {
144144

145145
public interface Reader extends Iterable<ZipFile.Entry> {
146146

147-
void extract(Path dstDir) throws IOException;
147+
void extract(Path dstDir);
148148

149-
void extract(Path dstDir, String fileName) throws IOException;
149+
void extract(Path dstDir, String fileName);
150150

151-
void extract(Path dstDir, Collection<String> fileNames) throws IOException;
151+
void extract(Path dstDir, Collection<String> fileNames);
152152

153-
ZipFile.Entry extract(String fileName) throws IOException;
153+
ZipFile.Entry extract(String fileName);
154154

155155
default Stream<Entry> stream() {
156156
return StreamSupport.stream(spliterator(), false);
@@ -165,11 +165,11 @@ default Stream<Entry> stream() {
165165

166166
public interface Info {
167167

168-
void printTextInfo(PrintStream out) throws IOException;
168+
void printTextInfo(PrintStream out);
169169

170-
void decompose(Path dir) throws IOException;
170+
void decompose(Path dir);
171171

172-
CentralDirectory.FileHeader getFileHeader(String entryName) throws IOException;
172+
CentralDirectory.FileHeader getFileHeader(String entryName);
173173
}
174174

175175
}

src/main/java/ru/olegcherednik/zip4jvm/ZipInfo.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import lombok.AccessLevel;
2626
import lombok.RequiredArgsConstructor;
2727

28-
import java.io.IOException;
2928
import java.io.PrintStream;
3029
import java.nio.file.Path;
3130
import java.util.Optional;
@@ -60,19 +59,19 @@ public ZipInfo password(char[] password) {
6059
return this;
6160
}
6261

63-
public void printShortInfo() throws IOException {
62+
public void printShortInfo() {
6463
printShortInfo(System.out);
6564
}
6665

67-
public void printShortInfo(PrintStream out) throws IOException {
66+
public void printShortInfo(PrintStream out) {
6867
ZipFile.info(srcZip, settings).printTextInfo(out);
6968
}
7069

71-
public void decompose(Path dstDir) throws IOException {
70+
public void decompose(Path dstDir) {
7271
ZipFile.info(srcZip, settings).decompose(dstDir);
7372
}
7473

75-
public CentralDirectory.FileHeader getFileHeader(String entryName) throws IOException {
74+
public CentralDirectory.FileHeader getFileHeader(String entryName) {
7675
return ZipFile.info(srcZip, settings).getFileHeader(entryName);
7776
}
7877

src/main/java/ru/olegcherednik/zip4jvm/engine/info/InfoEngine.java

+19-15
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import ru.olegcherednik.zip4jvm.model.block.BlockModel;
3333
import ru.olegcherednik.zip4jvm.model.settings.ZipInfoSettings;
3434
import ru.olegcherednik.zip4jvm.model.src.SrcZip;
35+
import ru.olegcherednik.zip4jvm.utils.quitely.Quietly;
3536

3637
import lombok.RequiredArgsConstructor;
3738

38-
import java.io.IOException;
3939
import java.io.PrintStream;
4040
import java.nio.file.Files;
4141
import java.nio.file.Path;
@@ -52,7 +52,7 @@ public final class InfoEngine implements ZipFile.Info {
5252

5353
@Override
5454
@SuppressWarnings("NonShortCircuitBooleanExpression")
55-
public void printTextInfo(PrintStream out) throws IOException {
55+
public void printTextInfo(PrintStream out) {
5656
BlockModel blockModel = createModel();
5757

5858
boolean emptyLine = new EndCentralDirectoryDecompose(blockModel, settings).printTextInfo(out, false);
@@ -62,15 +62,17 @@ public void printTextInfo(PrintStream out) throws IOException {
6262
}
6363

6464
@Override
65-
public void decompose(Path dir) throws IOException {
66-
Files.createDirectories(dir);
65+
public void decompose(Path dir) {
66+
Quietly.doRuntime(() -> {
67+
Files.createDirectories(dir);
6768

68-
BlockModel blockModel = createModel();
69+
BlockModel blockModel = createModel();
6970

70-
new EndCentralDirectoryDecompose(blockModel, settings).decompose(dir);
71-
new Zip64Decompose(blockModel, settings).decompose(dir);
72-
getCentralDirectoryDecompose(blockModel).decompose(dir);
73-
new ZipEntriesDecompose(blockModel, settings).decompose(dir);
71+
new EndCentralDirectoryDecompose(blockModel, settings).decompose(dir);
72+
new Zip64Decompose(blockModel, settings).decompose(dir);
73+
getCentralDirectoryDecompose(blockModel).decompose(dir);
74+
new ZipEntriesDecompose(blockModel, settings).decompose(dir);
75+
});
7476
}
7577

7678
private Decompose getCentralDirectoryDecompose(BlockModel blockModel) {
@@ -80,7 +82,7 @@ private Decompose getCentralDirectoryDecompose(BlockModel blockModel) {
8082
}
8183

8284
@Override
83-
public CentralDirectory.FileHeader getFileHeader(String entryName) throws IOException {
85+
public CentralDirectory.FileHeader getFileHeader(String entryName) {
8486
ZipModelReader reader = new ZipModelReader(srcZip,
8587
settings.getCustomizeCharset(),
8688
settings.getPasswordProvider(),
@@ -91,11 +93,13 @@ public CentralDirectory.FileHeader getFileHeader(String entryName) throws IOExce
9193
.findFirst().orElseThrow(() -> new EntryNotFoundException(entryName));
9294
}
9395

94-
public BlockModel createModel() throws IOException {
95-
BlockZipModelReader reader = new BlockZipModelReader(srcZip,
96-
settings.getCustomizeCharset(),
97-
settings.getPasswordProvider());
98-
return settings.isReadEntries() ? reader.readWithEntries() : reader.read();
96+
public BlockModel createModel() {
97+
return Quietly.doRuntime(() -> {
98+
BlockZipModelReader reader = new BlockZipModelReader(srcZip,
99+
settings.getCustomizeCharset(),
100+
settings.getPasswordProvider());
101+
return settings.isReadEntries() ? reader.readWithEntries() : reader.read();
102+
});
99103
}
100104

101105
}

src/main/java/ru/olegcherednik/zip4jvm/engine/unzip/UnzipEngine.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ public UnzipEngine(SrcZip srcZip, UnzipSettings settings) {
5353
// ---------- ZipFile.Reader ----------
5454

5555
@Override
56-
public void extract(Path dstDir) throws IOException {
56+
public void extract(Path dstDir) {
5757
extract(dstDir, Collections.emptySet());
5858
}
5959

6060
@Override
61-
public void extract(Path dstDir, String fileName) throws IOException {
61+
public void extract(Path dstDir, String fileName) {
6262
extract(dstDir, Collections.singleton(fileName));
6363
}
6464

6565
@Override
66-
public void extract(Path dstDir, Collection<String> fileNames) throws IOException {
66+
public void extract(Path dstDir, Collection<String> fileNames) {
6767
unzipExtractEngine.extract(dstDir, fileNames);
6868
}
6969

7070
@Override
71-
public ZipFile.Entry extract(String fileName) throws IOException {
71+
public ZipFile.Entry extract(String fileName) {
7272
return unzipExtractEngine.extract(fileName);
7373
}
7474

src/main/java/ru/olegcherednik/zip4jvm/engine/unzip/UnzipExtractEngine.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class UnzipExtractEngine {
6666
protected final PasswordProvider passwordProvider;
6767
protected final ZipModel zipModel;
6868

69-
public void extract(Path dstDir, Collection<String> fileNames) throws IOException {
69+
public void extract(Path dstDir, Collection<String> fileNames) {
7070
Map<String, String> map = null;
7171

7272
if (CollectionUtils.isNotEmpty(fileNames))
@@ -75,7 +75,7 @@ public void extract(Path dstDir, Collection<String> fileNames) throws IOExceptio
7575
extractEntry(dstDir, map);
7676
}
7777

78-
public ZipFile.Entry extract(String fileName) throws IOException {
78+
public ZipFile.Entry extract(String fileName) {
7979
ZipEntry zipEntry = zipModel.getZipEntryByFileName(ZipUtils.normalizeFileName(fileName));
8080
zipEntry.setPassword(passwordProvider.getFilePassword(zipEntry.getFileName()));
8181
return zipEntry.createImmutableEntry();
@@ -107,7 +107,7 @@ protected List<ZipEntry> getEntriesByPrefix(String prefix) {
107107

108108
// ----------
109109

110-
protected void extractEntry(Path dstDir, Map<String, String> map) throws IOException {
110+
protected void extractEntry(Path dstDir, Map<String, String> map) {
111111
try (ConsecutiveAccessDataInput in = createConsecutiveDataInput(zipModel.getSrcZip())) {
112112
Iterator<ZipEntry> it = zipModel.absOffsAscIterator();
113113

@@ -124,6 +124,8 @@ protected void extractEntry(Path dstDir, Map<String, String> map) throws IOExcep
124124
extractEntry(file, zipEntry, in);
125125
}
126126
}
127+
} catch (IOException e) {
128+
throw new Zip4jvmException(e);
127129
}
128130
}
129131

@@ -187,6 +189,7 @@ protected static OutputStream getOutputStream(Path file) throws IOException {
187189
public static ConsecutiveAccessDataInput createConsecutiveDataInput(SrcZip srcZip) throws IOException {
188190
return srcZip.isSolid() ? new SolidConsecutiveAccessDataInput(srcZip)
189191
: new SplitConsecutiveAccessDataInput(srcZip);
192+
190193
}
191194

192195
}

src/test/java/ru/olegcherednik/zip4jvm/ModifyCommentTest.java

+15-16
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@
4242
* @since 15.03.2019
4343
*/
4444
@Test
45-
@SuppressWarnings("FieldNamingConvention")
4645
public class ModifyCommentTest {
4746

48-
private static final Path rootDir = Zip4jvmSuite.generateSubDirNameWithTime(ModifyCommentTest.class);
49-
private static final Path zip = rootDir.resolve("src.zip");
47+
private static final Path ROOT_DIR = Zip4jvmSuite.generateSubDirNameWithTime(ModifyCommentTest.class);
48+
private static final Path SRC_ZIP = ROOT_DIR.resolve("src.zip");
5049

5150
@BeforeClass
5251
public static void createDir() throws IOException {
53-
Files.createDirectories(rootDir);
52+
Files.createDirectories(ROOT_DIR);
5453
}
5554

5655
@AfterClass(enabled = Zip4jvmSuite.clear)
5756
public static void removeDir() throws IOException {
58-
Zip4jvmSuite.removeDir(rootDir);
57+
Zip4jvmSuite.removeDir(ROOT_DIR);
5958
}
6059

6160
public void shouldCreateNewZipWithComment() throws IOException {
@@ -64,32 +63,32 @@ public void shouldCreateNewZipWithComment() throws IOException {
6463
ZipSettings settings = ZipSettings.builder()
6564
.entrySettings(entrySettings)
6665
.comment("Oleg Cherednik - Олег Чередник").build();
67-
ZipIt.zip(zip).settings(settings).add(fileOlegCherednik);
68-
assertThatZipFile(zip).exists().hasComment("Oleg Cherednik - Олег Чередник");
66+
ZipIt.zip(SRC_ZIP).settings(settings).add(fileOlegCherednik);
67+
assertThatZipFile(SRC_ZIP).exists().hasComment("Oleg Cherednik - Олег Чередник");
6968
}
7069

7170
@Test(dependsOnMethods = "shouldCreateNewZipWithComment")
7271
public void shouldAddCommentToExistedNoSplitZip() throws IOException {
73-
ZipMisc.zip(zip).setComment("this is new comment - новый комментарий");
74-
assertThatZipFile(zip).exists().hasComment("this is new comment - новый комментарий");
72+
ZipMisc.zip(SRC_ZIP).setComment("this is new comment - новый комментарий");
73+
assertThatZipFile(SRC_ZIP).exists().hasComment("this is new comment - новый комментарий");
7574
}
7675

7776
@Test(dependsOnMethods = "shouldAddCommentToExistedNoSplitZip")
7877
public void shouldClearCommentForExistedZip() throws IOException {
79-
ZipMisc.zip(zip).setComment(null);
80-
assertThatZipFile(zip).exists().hasCommentSize(0);
78+
ZipMisc.zip(SRC_ZIP).setComment(null);
79+
assertThatZipFile(SRC_ZIP).exists().hasCommentSize(0);
8180
}
8281

8382
@Test(dependsOnMethods = "shouldClearCommentForExistedZip")
8483
public void shouldAddCommentToEncryptedZip() throws IOException {
85-
assertThatZipFile(zip, Zip4jvmSuite.password).hasCommentSize(0);
84+
assertThatZipFile(SRC_ZIP, Zip4jvmSuite.password).hasCommentSize(0);
8685

87-
ZipMisc.zip(zip).setComment("this is new comment");
88-
assertThatZipFile(zip, Zip4jvmSuite.password).hasComment("this is new comment");
86+
ZipMisc.zip(SRC_ZIP).setComment("this is new comment");
87+
assertThatZipFile(SRC_ZIP, Zip4jvmSuite.password).hasComment("this is new comment");
8988
}
9089

9190
public void shouldSetCommentWithMaxLength() throws IOException {
92-
Path srcZip = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve("src.zip");
91+
Path srcZip = Zip4jvmSuite.subDirNameAsMethodName(ROOT_DIR).resolve("src.zip");
9392
Files.createDirectories(srcZip.getParent());
9493
Files.copy(zipDeflateSolid, srcZip);
9594

@@ -98,7 +97,7 @@ public void shouldSetCommentWithMaxLength() throws IOException {
9897
}
9998

10099
public void shouldThrowExceptionWhenCommentIsOverMaxLength() throws IOException {
101-
Path srcZip = Zip4jvmSuite.subDirNameAsMethodName(rootDir).resolve("src.zip");
100+
Path srcZip = Zip4jvmSuite.subDirNameAsMethodName(ROOT_DIR).resolve("src.zip");
102101
Files.createDirectories(srcZip.getParent());
103102
Files.copy(zipDeflateSolid, srcZip);
104103

src/test/java/ru/olegcherednik/zip4jvm/TestDataAssert.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public final class TestDataAssert {
100100

101101
public static final Consumer<IDirectoryAssert<?>> dirEmptyAssert = dir -> dir.exists().hasEntries(0);
102102

103-
public static final Consumer<IRegularFileAssert<?>> fileMcdonnelDouglasAssert =
103+
public static final Consumer<IRegularFileAssert<?>> fileMcDonnellDouglasAssert =
104104
file -> file.exists().hasSize(624_746).isImage().isContentEqualTo(fileMcdonnelDouglas);
105105
public static final Consumer<IRegularFileAssert<?>> fileSaintPetersburgAssert =
106106
file -> file.exists().hasSize(1_074_836).isImage().isContentEqualTo(fileSaintPetersburg);
@@ -118,7 +118,7 @@ public final class TestDataAssert {
118118
dirCarsAssert.accept(dir.directory(dirNameCars));
119119
dirEmptyAssert.accept(dir.directory(dirNameEmpty));
120120

121-
fileMcdonnelDouglasAssert.accept(dir.regularFile(fileNameMcdonnelDouglas));
121+
fileMcDonnellDouglasAssert.accept(dir.regularFile(fileNameMcdonnelDouglas));
122122
fileSaintPetersburgAssert.accept(dir.regularFile(fileNameSaintPetersburg));
123123
fileSigSauerAssert.accept(dir.regularFile(fileNameSigSauer));
124124
fileEmptyAssert.accept(dir.regularFile(fileNameEmpty));

0 commit comments

Comments
 (0)