Skip to content

Commit

Permalink
#13: Implemented update changelog in zip (if enabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
k3b committed Oct 31, 2018
1 parent 8c46398 commit 8a52166
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public long getLastModified() {
return MediaUtil.getDateModified(this.context, this.uri);
}


@Override
public String toString() {
return (this.uri != null) ? this.uri.toString() : super.toString();
public StringBuilder getLogEntry(StringBuilder _result) {
StringBuilder result = super.getLogEntry(_result);
result.append(FIELD_DELIMITER);
if (uri != null) result.append(uri);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ public DocumentFileCompressItem(Context context, String destZipPath, File srcFil
}

public InputStream getFileInputStream() throws IOException {
DocumentFile doc = DocumentFile.fromFile(getFile());
return context.getContentResolver().openInputStream(uri);
}

@Override
public StringBuilder getLogEntry(StringBuilder _result) {
StringBuilder result = super.getLogEntry(_result);
result.append(FIELD_DELIMITER);
if (uri != null) result.append(uri);
return result;
}

}
22 changes: 19 additions & 3 deletions libK3bZip/src/main/java/de/k3b/zip/CompressItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Created by k3b on 16.02.2015.
*/
abstract public class CompressItem {
protected static final String FIELD_DELIMITER = ";";
protected boolean processed;

/** the antry may contain path within zip file */
Expand All @@ -40,15 +41,15 @@ abstract public class CompressItem {

public boolean isSame(CompressItem other) {
if (other == null) return false;
return this.getClass().equals(other.getClass());
return this.zipEntryFileName.equals(other.zipEntryFileName);
}

/** the antry may contain path within zip file */
/** the entry may contain path within zip file */
public String getZipEntryFileName() {
return zipEntryFileName;
}

/** the antry may contain path within zip file */
/** the entry may contain path within zip file */
public CompressItem setZipEntryFileName(String zipEntryFileName) {
this.zipEntryFileName = zipEntryFileName;
return this;
Expand All @@ -70,4 +71,19 @@ public String getZipEntryComment() {
public void setZipEntryComment(String zipEntryComment) {
this.zipEntryComment = zipEntryComment;
}

public StringBuilder getLogEntry(StringBuilder _result) {
StringBuilder result = (_result == null) ? new StringBuilder() : _result;
result.append(getZipEntryFileName());
return result;
}

@Override
public String toString() {
StringBuilder result = getLogEntry(null);
result.insert(0, FIELD_DELIMITER);
result.insert(0,this.getClass().getSimpleName());
result.insert(0, processed ? "[v] " : "[ ] ");
return result.toString();
}
}
40 changes: 33 additions & 7 deletions libK3bZip/src/main/java/de/k3b/zip/CompressJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ public class CompressJob implements ZipLog {
public CompressJob(ZipLog zipLog, String fileLogInZip) {
this.zipLog = zipLog;
if (!StringUtils.isNullOrEmpty(fileLogInZip)) {
this.compressLogItem = addLofToCompressQue(fileLogInZip, null);
this.compressLogItem = addLog2CompressQue(fileLogInZip, null);

// do not process this item in qoueOutPutLoop
this.compressLogItem.setProcessed(true);
}
}


public static String readAll(InputStream is, byte[] buffer) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
copyStream(byteArrayOutputStream, is, buffer);
Expand Down Expand Up @@ -183,15 +185,16 @@ public TextCompressItem addTextToCompressQue(String textfile, String textToBeAdd
return this.compressTextItem;
}

public TextCompressItem addLofToCompressQue(String textfile, String textToBeAdded) {
public TextCompressItem addLog2CompressQue(String textfile, String textToBeAdded) {
this.compressLogItem = addTextToCompressQue(this.compressLogItem, textfile, textToBeAdded);
return this.compressLogItem;
}

private TextCompressItem addTextToCompressQue(TextCompressItem textItem, String textfile, String textToBeAdded) {
private TextCompressItem addTextToCompressQue(TextCompressItem textItem, String zipEntryPath,
String textToBeAdded) {
if (textItem == null) {
File srcFile = new File("/" + textfile);
textItem = new TextCompressItem("", srcFile, null);

textItem = new TextCompressItem(zipEntryPath, null);
textItem.setLastModified(new Date().getTime());
addToCompressQueue(textItem);
}
Expand Down Expand Up @@ -393,7 +396,8 @@ public int compress(boolean renameDuplicateTextFile) {

boolean preventTextFromRenaming = (!renameDuplicateTextFile) && (this.compressTextItem != null) && !this.compressTextItem.isProcessed();

if (compressQue.size() == 0) {
int emptyCount = (compressLogItem != null) ? 1 : 0;
if (compressQue.size() <= emptyCount) {
logger.debug("aboard: no (more) files to addToCompressQue to zip");
return RESULT_NO_CHANGES;
}
Expand Down Expand Up @@ -472,6 +476,10 @@ public int compress(boolean renameDuplicateTextFile) {
// itemCount++;
}

if (this.compressLogItem != null) {
this.compressLogItem.addText(";;;;;---");
}

// (1b) copy new compressQue
for (CompressItem item : this.compressQue) {
if (!item.isProcessed()) {
Expand All @@ -486,9 +494,27 @@ public int compress(boolean renameDuplicateTextFile) {
zipEntryInputStream = null;
itemCount++;
item.setProcessed(true);

if (this.compressLogItem != null) {
this.compressLogItem.addText(item.getLogEntry(null).toString());
}
}
}

if (compressLogItem != null) {
CompressItem item = compressLogItem;
String newFullDestZipItemName = item.getZipEntryFileName();
context = traceMessage("(1b) copy new item {0} as {1} to {2}",
item, newFullDestZipItemName, newZipFileName);
zipEntryInputStream = item.getFileInputStream();
ZipEntry zipEntry = createZipEntry(newFullDestZipItemName,
item.getLastModified(), item.getZipEntryComment());
add(zipOutputStream, zipEntry, null, zipEntryInputStream);
zipEntryInputStream.close();
zipEntryInputStream = null;
itemCount++;
}

zipOutputStream.close();
zipOutputStream = null;

Expand Down
19 changes: 14 additions & 5 deletions libK3bZip/src/main/java/de/k3b/zip/FileCompressItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
public class FileCompressItem extends CompressItem {
private File file;

public FileCompressItem(String destZipPath, File srcFile, String zipEntryComment) {
if (destZipPath == null) destZipPath = "";
/**
*
* @param destZipPathWithoutFileName directory with trailing "/" (without filename) where the entry goes to. null==root dir.
* @param srcFile full path to source file
* @param zipEntryComment
*/
public FileCompressItem(String destZipPathWithoutFileName, File srcFile, String zipEntryComment) {
if (destZipPathWithoutFileName == null) destZipPathWithoutFileName = "";
setFile(srcFile);
setZipEntryFileName(destZipPath + srcFile.getName());
setZipEntryFileName(destZipPathWithoutFileName + srcFile.getName());
setZipEntryComment(zipEntryComment);
}

Expand Down Expand Up @@ -60,8 +66,11 @@ public boolean isSame(CompressItem other) {
}

@Override
public String toString() {
return (this.file != null) ? this.file.toString() : super.toString();
public StringBuilder getLogEntry(StringBuilder _result) {
StringBuilder result = super.getLogEntry(_result);
result.append(FIELD_DELIMITER);
if (getFile() != null) result.append(getFile());
return result;
}

}
25 changes: 20 additions & 5 deletions libK3bZip/src/main/java/de/k3b/zip/TextCompressItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package de.k3b.zip;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -28,13 +27,14 @@
*
* Created by k3b on 24.11.2014.
*/
public class TextCompressItem extends FileCompressItem {
public class TextCompressItem extends CompressItem {

private StringBuilder text = new StringBuilder();
private long lastModified;

public TextCompressItem(String destZipPath, File srcFile, String zipEntryComment) {
super(destZipPath, srcFile, zipEntryComment);
public TextCompressItem(String zipEntryFileName, String zipEntryComment) {
setZipEntryFileName(zipEntryFileName);
setZipEntryComment(zipEntryComment);
}

public InputStream getFileInputStream() throws IOException {
Expand All @@ -43,7 +43,7 @@ public InputStream getFileInputStream() throws IOException {

public TextCompressItem addText(String text) {
if ((text != null) && (text.length() > 0)) {
this.text.append(text).append("\n\n");
this.text.append(text).append("\n");
}
return this;
}
Expand All @@ -59,4 +59,19 @@ public void setLastModified(long lastModified) {
public long getLastModified() {
return lastModified;
}

@Override
public StringBuilder getLogEntry(StringBuilder _result) {
StringBuilder result = super.getLogEntry(_result);
result.append(FIELD_DELIMITER);
if (text.length() > 0) {
String firstText = text.substring(0, Math.min(10, text.length() - 1));
result
.append("'")
.append(firstText.replaceAll("[\\n\\r\\t;\\\"\\\']+"," "))
.append("...'");
}
return result;
}

}
62 changes: 33 additions & 29 deletions libK3bZip/src/test/java/de/k3b/zip/CompressJobIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@
* Created by k3b on 03.11.2014.
*/
public class CompressJobIntegrationTests {
private static final int NUMBER_OF_LOG_ENTRIES = 1;
static private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HHmmss-S");
// static private File root = new File(System.getProperty("java.io.tmpdir")
static private String root = System.getProperty("java.io.tmpdir")

+ "/k3bZipTests/";
static private ZipStorage testZip = new ZipStorageFile(root+"test.zip");
static private File testContent = new File(root + "testFile.txt");
static private File testContent2 = new File(root + "testFile2.txt");
static private File testDirWith2SubItems = new File(root + "dir");
+ "/k3bZipTests/CompressJobIntegrationTests/";
static private String rootInput = root + "inputFiles/";
static private File testContent = new File(rootInput + "testFile.txt");
static private File testContent2 = new File(rootInput + "testFile2.txt");
static private File testDirWith2SubItems = new File(rootInput + "dir");
static private File testContent3 = new File(testDirWith2SubItems, "testFile3.txt");
static private File testContent4 = new File(testDirWith2SubItems, "testFile4.txt");

Expand All @@ -60,13 +60,14 @@ static public void createTestData() throws IOException, ParseException {
createTestFile(testContent2, new Date());
createTestFile(testContent3, format.parse("1981-12-24_123456-123"));
createTestFile(testContent4, format.parse("1982-12-24_123456-123"));

System.out.println("CompressJobIntegrationTests: files in " + root);
}

private static void createTestFile(File testContent, Date fileDate) throws IOException {
OutputStream testContentFile = new FileOutputStream(testContent);

final String someContent = "some test data for " +
testZip;
final String someContent = "some test data";
InputStream someContentStream = new ByteArrayInputStream(someContent.getBytes("UTF-8"));

CompressJob.copyStream(
Expand All @@ -77,67 +78,70 @@ private static void createTestFile(File testContent, Date fileDate) throws IOExc
testContent.setLastModified(fileDate.getTime());
}

@Before
public void setup() throws IOException {
testZip.delete(ZipStorage.ZipInstance.current);
CompressJob sut = createCompressJob(testZip);
sut.addToCompressQue("", testContent.getAbsolutePath());
int itemCount = sut.compress(false);
Assert.assertEquals(1, itemCount);
private CompressJob createCompressJob(ZipStorage testZip) {
return new CompressJob(null, "changeHistory.txt").setDestZipFile(testZip);
}

private CompressJob createCompressJob(ZipStorage testZip) {
return new CompressJob(null, null).setDestZipFile(testZip);
private CompressJob createCompressJob(String testName) {
ZipStorage testZip = new ZipStorageFile(root+ testName + ".zip");
testZip.delete(ZipStorage.ZipInstance.current);

CompressJob initialContent = createCompressJob(testZip);
initialContent.addToCompressQue("", testContent.getAbsolutePath());
int itemCount = initialContent.compress(false);
Assert.assertEquals("exampleItem + log == 2", 2, itemCount);

return createCompressJob(testZip);
}

@Test
public void shouldNotAddDuplicate() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldNotAddDuplicate");
sut.addToCompressQue("", testContent.getAbsolutePath());
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(CompressJob.RESULT_NO_CHANGES, itemCount);
}

@Test
public void shouldAppendDifferentFile() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldAppendDifferentFile");
sut.addToCompressQue("", testContent2.getAbsolutePath());
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(1, itemCount);
}

@Test
public void shouldAppendDir() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldAppendDir");
sut.addToCompressQue("", testDirWith2SubItems);
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(2, itemCount);
}

@Test
public void shouldRenameSameFileNameWithDifferentDate() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldRenameSameFileNameWithDifferentDate");
CompressItem item = sut.addToCompressQue("", testContent2);
item.setZipEntryFileName(testContent.getName());
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(1, itemCount);
Assert.assertEquals("testFile(1).txt", item.getZipEntryFileName());
}

@Test
public void shouldAppendTextAsFile() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldAppendTextAsFile");
sut.addTextToCompressQue("hello.txt", "hello world");
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(1, itemCount);
}

@Test
public void shouldAppendTextAsFileToExisting() {
CompressJob sut = createCompressJob(testZip);
CompressJob sut = createCompressJob("shouldAppendTextAsFileToExisting");
sut.addTextToCompressQue("hello.txt", "hello world");
sut.addTextToCompressQue("hello.txt", "once again: hello world");
int itemCount = sut.compress(false);
int itemCount = sut.compress(false) - NUMBER_OF_LOG_ENTRIES;
Assert.assertEquals(1, itemCount);
}
}

0 comments on commit 8a52166

Please sign in to comment.