Merge pull request #497 from dicej/deflateoutputstream

update ZipOutputStreamTest to use write(byte[]) function as well as others
This commit is contained in:
Joshua Warner 2016-09-04 15:07:47 +00:00 committed by GitHub
commit cbcc15bf74

View File

@ -12,199 +12,203 @@ import java.util.zip.*;
public class ZipOutputStreamTest public class ZipOutputStreamTest
{ {
private static final String TEST1 = "test1.txt"; private static final String TEST1 = "test1.txt";
private static final String TEST2 = "test2.txt"; private static final String TEST2 = "test2.txt";
private static final String TEST3 = "test3.txt"; private static final String TEST3 = "test3.txt";
private static final String TEST4 = "test4.txt"; private static final String TEST4 = "test4.txt";
private static final String TEST1_CONTENTS = "\"this is a test\""; private static final String TEST1_CONTENTS = "\"this is a test\"";
private static final String TEST2_CONTENTS = "this is a\nmulti-line test"; private static final String TEST2_CONTENTS = "this is a\nmulti-line test";
private static final String TEST3_CONTENTS = "74 68 69 73 20 69 73 20 61 20 74 65 73 74"; private static final String TEST3_CONTENTS = "74 68 69 73 20 69 73 20 61 20 74 65 73 74";
private static final String TEST4_CONTENTS = "01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100"; private static final String TEST4_CONTENTS = "01110100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100";
private static final String ONE_PARAM_ZIP_PREFIX = "zos1param";
private static final String THREE_PARAM_ZIP_PREFIX = "zos3param";
private static final String ZIP_SUFFIX = ".zip";
private static final Map<String, String> FILES_CONTENTS; private static final String BYTE_ZIP_PREFIX = "zosByte";
static private static final String ARRAY_ZIP_PREFIX = "zosArray";
{ private static final String ARRAY_OFFSET_LENGTH_ZIP_PREFIX = "zosArrayOffsetLength";
Map<String, String> m = new HashMap<String, String>(); private static final String ZIP_SUFFIX = ".zip";
m.put(TEST1, TEST1_CONTENTS);
m.put(TEST2, TEST2_CONTENTS);
m.put(TEST3, TEST3_CONTENTS);
m.put(TEST4, TEST4_CONTENTS);
FILES_CONTENTS = Collections.unmodifiableMap(m);
}
private static final boolean USE_ONE_PARAM_WRITE = true; private static final Map<String, String> FILES_CONTENTS;
private static final boolean USE_THREE_PARAM_WRITE = false; static
private static byte[] buffer = new byte[1024]; {
Map<String, String> m = new HashMap<String, String>();
m.put(TEST1, TEST1_CONTENTS);
m.put(TEST2, TEST2_CONTENTS);
m.put(TEST3, TEST3_CONTENTS);
m.put(TEST4, TEST4_CONTENTS);
FILES_CONTENTS = Collections.unmodifiableMap(m);
}
public static void main(String[] args) private static enum WriteStyle {
{ Byte(ARRAY_ZIP_PREFIX),
List<File> zipFiles = new ArrayList<File>(2); Array(ARRAY_ZIP_PREFIX),
ArrayOffsetLength(ARRAY_OFFSET_LENGTH_ZIP_PREFIX);
// Test 1-param write function public final String prefix;
File f1 = createZip(USE_ONE_PARAM_WRITE);
zipFiles.add(f1);
verifyContents(f1.getAbsolutePath());
// Test 3-param write function
File f2 = createZip(USE_THREE_PARAM_WRITE);
zipFiles.add(f2);
verifyContents(f2.getAbsolutePath());
// Remove the created zip files
cleanUp(zipFiles);
}
private static File createZip(boolean useOneParam) private WriteStyle(String prefix) {
{ this.prefix = prefix;
FileOutputStream outputStream = null; }
ZipOutputStream zipContents = null; }
private static byte[] buffer = new byte[1024];
try private static void expect(boolean v) {
{ if (! v) throw new RuntimeException();
// Create a temporary zip file for this test }
String prefix = useOneParam ? ONE_PARAM_ZIP_PREFIX : THREE_PARAM_ZIP_PREFIX;
File outputZip = File.createTempFile(prefix, ZIP_SUFFIX);
System.out.println("Created " + outputZip.getAbsolutePath()); public static void main(String[] args)
throws Exception
{
List<File> zipFiles = new ArrayList<File>(2);
// Prepare the streams try {
outputStream = new FileOutputStream(outputZip); // Test byte-at-a-time write function
zipContents = new ZipOutputStream(outputStream); File f1 = createZip(WriteStyle.Byte);
zipFiles.add(f1);
verifyContents(f1.getAbsolutePath());
// Test arraw write function
File f2 = createZip(WriteStyle.Array);
zipFiles.add(f2);
verifyContents(f2.getAbsolutePath());
// Test arraw write function
File f3 = createZip(WriteStyle.ArrayOffsetLength);
zipFiles.add(f3);
verifyContents(f3.getAbsolutePath());
} finally {
// Remove the created zip files
cleanUp(zipFiles);
}
}
// Zip the file contents (convert directly from string to bytes) private static File createZip(WriteStyle writeStyle)
long startTime = System.currentTimeMillis(); throws Exception
for (Map.Entry<String, String> f : FILES_CONTENTS.entrySet()) {
{ FileOutputStream outputStream = null;
String name = f.getKey(); ZipOutputStream zipContents = null;
String contents = f.getValue();
System.out.println("Zipping " + name + "..."); try
ZipEntry entry = new ZipEntry(name); {
zipContents.putNextEntry(entry); // Create a temporary zip file for this test
String prefix = writeStyle.prefix;
File outputZip = File.createTempFile(prefix, ZIP_SUFFIX);
byte[] bytesToWrite = contents.getBytes(); System.out.println("Created " + outputZip.getAbsolutePath());
if (useOneParam) // Prepare the streams
{ outputStream = new FileOutputStream(outputZip);
// Use the 1-parameter write method; takes a single byte zipContents = new ZipOutputStream(outputStream);
for (int i = 0; i < bytesToWrite.length; i++)
{
zipContents.write(bytesToWrite[i]);
}
}
else
{
// Use 3-parameter write method; takes a buffer, offset, and length
zipContents.write(bytesToWrite, 0 , bytesToWrite.length);
}
// Done with this file // Zip the file contents (convert directly from string to bytes)
zipContents.closeEntry(); long startTime = System.currentTimeMillis();
System.out.println("Done"); for (Map.Entry<String, String> f : FILES_CONTENTS.entrySet())
} {
String name = f.getKey();
String contents = f.getValue();
// All files have been written System.out.println("Zipping " + name + "...");
long endTime = System.currentTimeMillis(); ZipEntry entry = new ZipEntry(name);
System.out.println("Finished " + outputZip.getName() + " in " + ((endTime - startTime) / 1000.0) + " seconds"); zipContents.putNextEntry(entry);
return outputZip;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
try
{
if (zipContents != null)
zipContents.close();
if (outputStream != null)
outputStream.close();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
private static void verifyContents(String zipName) byte[] bytesToWrite = contents.getBytes();
{
System.out.println("Verify " + zipName);
ZipFile zf = null;
BufferedReader reader = null;
int numFilesInZip = 0;
try switch (writeStyle) {
{ case Byte: {
String line; // Use the 1-parameter write method; takes a single byte
String contents; for (int i = 0; i < bytesToWrite.length; i++)
{
zipContents.write(bytesToWrite[i]);
}
} break;
// Get the contents of each file in the zip case Array: {
zf = new ZipFile(zipName); // Use 3-parameter write method; takes a buffer, offset, and length
for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();) zipContents.write(bytesToWrite);
{ } break;
ZipEntry entry = e.nextElement();
reader = new BufferedReader(new InputStreamReader(zf.getInputStream(entry)));
contents = "";
numFilesInZip += 1;
while ((line = reader.readLine()) != null) case ArrayOffsetLength: {
{ // Use 3-parameter write method; takes a buffer, offset, and length
if (contents.length() > 0) zipContents.write(bytesToWrite, 0 , bytesToWrite.length);
{ } break;
contents += "\n";
}
contents += line;
}
reader.close();
// Assert that this file's contents are correct default: throw new RuntimeException("unexpected write style: " + writeStyle);
assert(contents.equals(FILES_CONTENTS.get(entry.getName()))); }
}
zf.close();
// Assert that the zip contained the correct number of files // Done with this file
assert(numFilesInZip == FILES_CONTENTS.size()); zipContents.closeEntry();
} System.out.println("Done");
catch (Exception e) }
{
throw new RuntimeException(e);
}
finally
{
try
{
if (zf != null)
zf.close();
if (reader != null)
reader.close();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
private static void cleanUp(List<File> zipFiles) // All files have been written
{ long endTime = System.currentTimeMillis();
try System.out.println("Finished " + outputZip.getName() + " in " + ((endTime - startTime) / 1000.0) + " seconds");
{ return outputZip;
for (File f : zipFiles) }
{ finally
if (f.exists()) {
{ if (zipContents != null)
f.delete(); zipContents.close();
} if (outputStream != null)
} outputStream.close();
} }
catch (Exception e) }
{
throw new RuntimeException(e); private static void verifyContents(String zipName)
} throws Exception
} {
System.out.println("Verify " + zipName);
ZipFile zf = null;
BufferedReader reader = null;
int numFilesInZip = 0;
try
{
String line;
String contents;
// Get the contents of each file in the zip
zf = new ZipFile(zipName);
for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements();)
{
ZipEntry entry = e.nextElement();
reader = new BufferedReader(new InputStreamReader(zf.getInputStream(entry)));
contents = "";
numFilesInZip += 1;
while ((line = reader.readLine()) != null)
{
if (contents.length() > 0)
{
contents += "\n";
}
contents += line;
}
reader.close();
// Assert that this file's contents are correct
expect(contents.equals(FILES_CONTENTS.get(entry.getName())));
}
zf.close();
// Assert that the zip contained the correct number of files
expect(numFilesInZip == FILES_CONTENTS.size());
}
finally
{
if (zf != null)
zf.close();
if (reader != null)
reader.close();
}
}
private static void cleanUp(List<File> zipFiles)
throws Exception
{
for (File f : zipFiles)
{
if (f.exists())
{
f.delete();
}
}
}
} }