Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional handling of Files in TarEntries #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ target/
.settings/
.project
.classpath

.idea/
*.iml
4 changes: 4 additions & 0 deletions src/main/java/org/kamranzafar/jtar/TarEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ public File getFile() {
return this.file;
}

void setFile(File file){
this.file = file;
}

public long getSize() {
return header.size;
}
Expand Down
35 changes: 31 additions & 4 deletions src/main/java/org/kamranzafar/jtar/TarInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

package org.kamranzafar.jtar;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;

/**
* @author Kamran Zafar
Expand Down Expand Up @@ -145,6 +143,7 @@ public TarEntry getNextEntry() throws IOException {

if (!eof) {
currentEntry = new TarEntry(header);
readCurrentEntryIntoFile();
}

return currentEntry;
Expand All @@ -157,7 +156,35 @@ public TarEntry getNextEntry() throws IOException {
public long getCurrentOffset() {
return bytesRead;
}


/**
* Reads the contents of the current entry's file into a File object
* @throws IOException
*/
protected void readCurrentEntryIntoFile() throws IOException {

//Create a spot on disk to store the data
File tempFile = File.createTempFile("jtar",".tmp");
tempFile.deleteOnExit();

int count;
byte data[] = new byte[2048];

try(FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream dest = new BufferedOutputStream(fos)) {

while ((count = this.read(data,0,1)) != -1) {
dest.write(data, 0, count);
}

dest.flush();
}

if(currentEntry != null){
currentEntry.setFile(tempFile);
}
}

/**
* Closes the current tar entry
*
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/kamranzafar/jtar/TarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package org.kamranzafar.jtar;

import java.io.File;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* @author Kamran
Expand Down Expand Up @@ -93,4 +95,22 @@ public static String trim(String s, char c) {

return tmp.toString();
}

/**
* Returns a list of Files in a given tar archive.
*
* @param tarFile the tar archive File
* @return a list of Files in the tarFile parameter
* @throws IOException if there is an error reading the archive
*/
public static List<TarEntry> getAllEntriesFromTar(File tarFile) throws IOException{
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(tarFile)));
List<TarEntry> entries = new ArrayList<>();
TarEntry entry;
while((entry = tis.getNextEntry()) != null) {
entries.add(entry);
}

return entries;
}
}
3 changes: 2 additions & 1 deletion src/test/java/org/kamranzafar/jtar/JTarAppendTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private void untar() throws FileNotFoundException, IOException {
int count;
byte data[] = new byte[2048];
try (BufferedOutputStream dest = new BufferedOutputStream(new FileOutputStream(outDir + "/" + entry.getName()))) {
while ((count = in.read(data)) != -1) {
FileInputStream fis = new FileInputStream(entry.getFile());
while ((count = fis.read(data)) != -1) {
dest.write(data, 0, count);
}
}
Expand Down
47 changes: 33 additions & 14 deletions src/test/java/org/kamranzafar/jtar/JTarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.nio.file.Files;
import java.util.zip.GZIPInputStream;

Expand All @@ -37,6 +29,8 @@

public class JTarTest {
static final int BUFFER = 2048;
private static final String FIRST_FILE_CONTENTS = "HPeX2kD5kSTc7pzCDX";
private static final String LAST_FILE_CONTENTS = "jrPYpzLfWB5vZTRsSKqFvVj";

private File dir;

Expand All @@ -59,12 +53,12 @@ public void tar() throws IOException {
File tartest = new File(dir.getAbsolutePath(), "tartest");
tartest.mkdirs();

TestUtils.writeStringToFile("HPeX2kD5kSTc7pzCDX", new File(tartest, "one"));
TestUtils.writeStringToFile(FIRST_FILE_CONTENTS, new File(tartest, "one"));
TestUtils.writeStringToFile("gTzyuQjfhrnyX9cTBSy", new File(tartest, "two"));
TestUtils.writeStringToFile("KG889vdgjPHQXUEXCqrr", new File(tartest, "three"));
TestUtils.writeStringToFile("CNBDGjEJNYfms7rwxfkAJ", new File(tartest, "four"));
TestUtils.writeStringToFile("tT6mFKuLRjPmUDjcVTnjBL", new File(tartest, "five"));
TestUtils.writeStringToFile("jrPYpzLfWB5vZTRsSKqFvVj", new File(tartest, "six"));
TestUtils.writeStringToFile(LAST_FILE_CONTENTS, new File(tartest, "six"));

tarFolder(null, dir.getAbsolutePath() + "/tartest/", out);

Expand Down Expand Up @@ -115,6 +109,28 @@ public void untarTarFileDefaultSkip() throws IOException {

}

/**
* Untar the archive and verify that a File object is present for each entry
*
* @throws IOException
*/
@Test
public void untarTarArchiveAndGetFiles() throws IOException {
File zf = new File("src/test/resources/tartest.tar");
TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(zf)));
TarEntry entry;
while((entry = tis.getNextEntry()) != null) {
//Verify that we have a File object
assertTrue(entry.getFile() != null);
try(FileInputStream fis = new FileInputStream(entry.getFile())){
//Verify there is data in the file object
assertTrue(fis.read() != -1);
}
}
}



/**
* Untar the gzipped-tar file
*
Expand Down Expand Up @@ -144,11 +160,13 @@ public void testOffset() throws IOException {

TarInputStream tis = new TarInputStream(new BufferedInputStream(new FileInputStream(zf)));
tis.getNextEntry();
assertEquals(TarConstants.HEADER_BLOCK, tis.getCurrentOffset());
//At this point we have read the header and one file
assertEquals(TarConstants.HEADER_BLOCK + LAST_FILE_CONTENTS.length(), tis.getCurrentOffset());
tis.getNextEntry();
TarEntry entry = tis.getNextEntry();
// All of the files in the tartest.tar file are smaller than DATA_BLOCK
assertEquals(TarConstants.HEADER_BLOCK * 3 + TarConstants.DATA_BLOCK * 2, tis.getCurrentOffset());
assertEquals(TarConstants.HEADER_BLOCK * 3 + TarConstants.DATA_BLOCK * 2 + FIRST_FILE_CONTENTS.length() + 1
, tis.getCurrentOffset());
tis.close();

RandomAccessFile rif = new RandomAccessFile(zf, "r");
Expand Down Expand Up @@ -178,10 +196,11 @@ private void untar(TarInputStream tis, String destFolder) throws IOException {
}
}

FileInputStream fis = new FileInputStream(entry.getFile());
FileOutputStream fos = new FileOutputStream(destFolder + "/" + entry.getName());
dest = new BufferedOutputStream(fos);

while ((count = tis.read(data)) != -1) {
while ((count = fis.read(data)) != -1) {
dest.write(data, 0, count);
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/kamranzafar/jtar/TarUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.kamranzafar.jtar;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.*;
/**
* Author: Phillip Johnson
* Date: 5/6/14
*/
public class TarUtilsTest {

@Test
public void allFilesAreLoadedIntoCollection() throws IOException{
List<TarEntry> entries = TarUtils.getAllEntriesFromTar(new File("src/test/resources/tartest.tar"));
assertTrue(entries.size()==6);
List<String> fileNames = new ArrayList<>();
for(TarEntry entry : entries){
fileNames.add(entry.getName());
}
//Verify we saved each of the distinct entries
assertTrue(fileNames.contains("tartest/one"));
assertTrue(fileNames.contains("tartest/two"));
assertTrue(fileNames.contains("tartest/three"));
assertTrue(fileNames.contains("tartest/four"));
assertTrue(fileNames.contains("tartest/five"));
assertTrue(fileNames.contains("tartest/six"));
}
}