Skip to content

Commit

Permalink
#324 Downgrade to JDK 7
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Jun 22, 2021
1 parent 3e422d7 commit 2cb5358
Show file tree
Hide file tree
Showing 18 changed files with 201 additions and 96 deletions.
12 changes: 10 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
</licenses>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<junit.version>4.13.1</junit.version>
<lombok.version>1.18.8</lombok.version>
<assertj.version>3.12.2</assertj.version>
Expand Down Expand Up @@ -98,6 +98,14 @@
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/lingala/zip4j/headers/HeaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static net.lingala.zip4j.util.InternalZipConstants.ZIP4J_DEFAULT_CHARSET;
import static net.lingala.zip4j.util.InternalZipConstants.ZIP_STANDARD_CHARSET_NAME;
Expand Down Expand Up @@ -70,7 +70,13 @@ public static List<FileHeader> getFileHeadersUnderDirectory(List<FileHeader> all
return Collections.emptyList();
}

return allFileHeaders.stream().filter(e -> e.getFileName().startsWith(rootFileHeader.getFileName())).collect(Collectors.toList());
List<FileHeader> fileHeadersUnderDirectory = new ArrayList<>();
for (FileHeader fileHeader : allFileHeaders) {
if (fileHeader.getFileName().startsWith(rootFileHeader.getFileName())) {
fileHeadersUnderDirectory.add(fileHeader);
}
}
return fileHeadersUnderDirectory;
}

public static long getTotalUncompressedSizeOfAllFileHeaders(List<FileHeader> fileHeaders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

Expand Down Expand Up @@ -75,12 +76,15 @@ long copyFile(RandomAccessFile randomAccessFile, OutputStream outputStream, long
List<FileHeader> cloneAndSortFileHeadersByOffset(List<FileHeader> allFileHeaders) {
List<FileHeader> clonedFileHeaders = new ArrayList<>(allFileHeaders);
//noinspection Java8ListSort
Collections.sort(clonedFileHeaders, (o1, o2) -> {
if (o1.getFileName().equals(o2.getFileName())) {
return 0;
Collections.sort(clonedFileHeaders, new Comparator<FileHeader>() {
@Override
public int compare(FileHeader o1, FileHeader o2) {
if (o1.getFileName().equals(o2.getFileName())) {
return 0;
}

return o1.getOffsetLocalHeader() < o2.getOffsetLocalHeader() ? -1 : 1;
}

return o1.getOffsetLocalHeader() < o2.getOffsetLocalHeader() ? -1 : 1;
});

return clonedFileHeaders;
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/net/lingala/zip4j/tasks/AsyncZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public AsyncZipTask(AsyncTaskParameters asyncTaskParameters) {
this.executorService = asyncTaskParameters.executorService;
}

public void execute(T taskParameters) throws ZipException {
public void execute(final T taskParameters) throws ZipException {
if (runInThread && ProgressMonitor.State.BUSY.equals(progressMonitor.getState())) {
throw new ZipException("invalid operation - Zip4j is in busy state");
}
Expand All @@ -29,13 +29,16 @@ public void execute(T taskParameters) throws ZipException {
long totalWorkToBeDone = calculateTotalWork(taskParameters);
progressMonitor.setTotalWork(totalWorkToBeDone);

executorService.execute(() -> {
try {
performTaskWithErrorHandling(taskParameters, progressMonitor);
} catch (ZipException e) {
//Do nothing. Exception will be passed through progress monitor
} finally {
executorService.shutdown();
executorService.execute(new Runnable() {
@Override
public void run() {
try {
AsyncZipTask.this.performTaskWithErrorHandling(taskParameters, progressMonitor);
} catch (ZipException e) {
//Do nothing. Exception will be passed through progress monitor
} finally {
executorService.shutdown();
}
}
});
} else {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/net/lingala/zip4j/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.lingala.zip4j.progress.ProgressMonitor;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
Expand Down Expand Up @@ -381,8 +382,13 @@ public static String getFileExtension(File file) {
* @return sorted list of split files. Returns an empty list if no files of that pattern are found in the current directory
*/
public static File[] getAllSortedNumberedSplitFiles(File firstNumberedFile) {
String zipFileNameWithoutExtension = FileUtils.getFileNameWithoutExtension(firstNumberedFile.getName());
File[] allSplitFiles = firstNumberedFile.getParentFile().listFiles((dir, name) -> name.startsWith(zipFileNameWithoutExtension + "."));
final String zipFileNameWithoutExtension = FileUtils.getFileNameWithoutExtension(firstNumberedFile.getName());
File[] allSplitFiles = firstNumberedFile.getParentFile().listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(zipFileNameWithoutExtension + ".");
}
});

if(allSplitFiles == null) {
return new File[0];
Expand Down
23 changes: 14 additions & 9 deletions src/test/java/net/lingala/zip4j/AbstractIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public abstract class AbstractIT {
public void before() throws IOException {
generatedZipFile = temporaryFolder.newFile("output.zip");
outputFolder = temporaryFolder.newFolder("output");
File[] allTempFiles = temporaryFolder.getRoot().listFiles();
Arrays.stream(allTempFiles).forEach(File::delete);
cleanupDirectory(temporaryFolder.getRoot());
}

protected ZipParameters createZipParameters(EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) {
Expand Down Expand Up @@ -73,13 +72,7 @@ protected File getTestArchiveFromResources(String archiveName) {
}

protected void cleanupOutputFolder() {
File[] filesInOutputFolder = outputFolder.listFiles();

if (filesInOutputFolder == null) {
return;
}

Arrays.stream(filesInOutputFolder).forEach(File::delete);
cleanupDirectory(outputFolder);
}

protected Zip4jConfig buildDefaultConfig() {
Expand All @@ -93,4 +86,16 @@ protected Zip4jConfig buildConfig(Charset charset) {
protected Zip4jConfig buildConfig(int bufferSize) {
return new Zip4jConfig(null, bufferSize);
}

private void cleanupDirectory(File directory) {
File[] allTempFiles = directory.listFiles();
if (allTempFiles == null) {
return;
}
for (File file : allTempFiles) {
if (!file.delete()) {
throw new RuntimeException("Could not clean up directory. Error deleting file: " + file);
}
}
}
}
46 changes: 32 additions & 14 deletions src/test/java/net/lingala/zip4j/AddFilesToZipIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.AESExtraDataRecord;
import net.lingala.zip4j.model.AbstractFileHeader;
import net.lingala.zip4j.model.ExcludeFileFilter;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.LocalFileHeader;
import net.lingala.zip4j.model.ZipParameters;
Expand All @@ -29,13 +30,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static net.lingala.zip4j.testutils.HeaderVerifier.verifyLocalFileHeaderUncompressedSize;
import static net.lingala.zip4j.testutils.TestUtils.getFileNamesOfFiles;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

public class AddFilesToZipIT extends AbstractIT {

Expand Down Expand Up @@ -354,7 +355,7 @@ public void testAddFilesWithAes256EncryptionV1() throws IOException {
zipFile.addFiles(FILES_TO_ADD, zipParameters);

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size());
List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
List<String> fileNames = getFileNamesOfFiles(FILES_TO_ADD);
verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.AES,
AesKeyStrength.KEY_STRENGTH_256, AesVersion.ONE);
}
Expand All @@ -367,7 +368,7 @@ public void testAddFilesWithAes256EncryptionV2() throws IOException {
zipFile.addFiles(FILES_TO_ADD, zipParameters);

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size());
List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
List<String> fileNames = getFileNamesOfFiles(FILES_TO_ADD);
verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.AES,
AesKeyStrength.KEY_STRENGTH_256);
}
Expand All @@ -380,7 +381,7 @@ public void testAddFilesWithZipStandardEncryption() throws IOException {
zipFile.addFiles(FILES_TO_ADD, zipParameters);

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, PASSWORD, outputFolder, FILES_TO_ADD.size());
List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
List<String> fileNames = getFileNamesOfFiles(FILES_TO_ADD);
verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.ZIP_STANDARD,
null);
}
Expand All @@ -398,7 +399,7 @@ public void testAddFilesWhenFilesAlreadyExistsRemovesFiles() throws IOException
zipFile.addFiles(FILES_TO_ADD, zipParameters);

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, newPassword, outputFolder, FILES_TO_ADD.size());
List<String> fileNames = FILES_TO_ADD.stream().map(File::getName).collect(Collectors.toList());
List<String> fileNames = getFileNamesOfFiles(FILES_TO_ADD);
verifyZipFileContainsFiles(generatedZipFile, fileNames, CompressionMethod.DEFLATE, EncryptionMethod.ZIP_STANDARD,
null);
}
Expand Down Expand Up @@ -645,13 +646,18 @@ public void testAddFolderWithNotNormalizedPath() throws IOException {
@Test
public void testAddFolderWithExcludeFileFilter() throws IOException {
ZipFile zipFile = new ZipFile(generatedZipFile);
List<File> filesToExclude = Arrays.asList(
final List<File> filesToExclude = Arrays.asList(
TestUtils.getTestFileFromResources("sample.pdf"),
TestUtils.getTestFileFromResources("sample_directory/favicon.ico")
);
ZipParameters zipParameters = new ZipParameters();
zipParameters.setIncludeRootFolder(false);
zipParameters.setExcludeFileFilter(filesToExclude::contains);
zipParameters.setExcludeFileFilter(new ExcludeFileFilter() {
@Override
public boolean isExcluded(File o) {
return filesToExclude.contains(o);
}
});

zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters);

Expand Down Expand Up @@ -933,7 +939,11 @@ private void verifyFoldersInZip(List<FileHeader> fileHeaders, File generatedZipF
}

private void verifyFoldersInFileHeaders(List<FileHeader> fileHeaders) {
fileHeaders.stream().filter(FileHeader::isDirectory).forEach(this::verifyFolderEntryInZip);
for (FileHeader fileHeader : fileHeaders){
if (fileHeader.isDirectory()) {
verifyFolderEntryInZip(fileHeader);
}
}
}

private void verifyFoldersInLocalFileHeaders(File generatedZipFile, char[] password) throws IOException {
Expand All @@ -957,11 +967,15 @@ private void verifyFolderEntryInZip(AbstractFileHeader fileHeader) {
}

private void verifyAllFilesInZipContainsPath(List<FileHeader> fileHeaders, String pathToBeChecked) {
fileHeaders.forEach(e -> assertThat(e.getFileName()).startsWith(pathToBeChecked));
for (FileHeader fileHeader : fileHeaders) {
assertThat(fileHeader.getFileName()).startsWith(pathToBeChecked);
}
}

private void verifyAllFilesInZipDoesNotContainPath(List<FileHeader> fileHeaders, String pathToBeChecked) {
fileHeaders.forEach(e -> assertThat(e.getFileName()).doesNotStartWith(pathToBeChecked));
for (FileHeader fileHeader : fileHeaders) {
assertThat(fileHeader.getFileName()).doesNotStartWith(pathToBeChecked);
}
}

private void verifyAllFilesAreOf(List<FileHeader> fileHeaders, CompressionMethod compressionMethod,
Expand Down Expand Up @@ -1017,9 +1031,13 @@ private void verifyFileIsOf(File generatedZipFile, String fileName, CompressionM
}

private FileHeader getFileHeaderFrom(List<FileHeader> fileHeaders, String fileName) {
Optional<FileHeader> fileHeader = fileHeaders.stream().filter(e -> e.getFileName().equals(fileName)).findFirst();
assertThat(fileHeader).isPresent();
return fileHeader.get();
for (FileHeader fileHeader : fileHeaders) {
if (fileHeader.getFileName().equals(fileName)) {
return fileHeader;
}
}
fail("Could not find a file header by filename: " + fileName);
return null;
}

private List<FileHeader> getFileHeaders(File generatedZipFile) throws ZipException {
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/net/lingala/zip4j/CreateZipFileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,13 @@ private void verifyAesExtraDataRecord(AESExtraDataRecord aesExtraDataRecord, Com
assertThat(aesExtraDataRecord.getAesKeyStrength()).isEqualTo(aesKeyStrength);
}

private File[] getAllSplitZipFilesInFolder(File folder, String fileNameWithoutExtension) {
FilenameFilter filenameFilter = (dir, name) -> name.contains(fileNameWithoutExtension + ".");
private File[] getAllSplitZipFilesInFolder(File folder, final String fileNameWithoutExtension) {
FilenameFilter filenameFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.contains(fileNameWithoutExtension + ".");
}
};
return folder.listFiles(filenameFilter);
}

Expand Down
Loading

0 comments on commit 2cb5358

Please sign in to comment.