Skip to content

Commit 6484ebb

Browse files
author
Plamen Totev
committed
Add method to compare zip and tar files
1 parent b8b12c8 commit 6484ebb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/test/java/org/codehaus/plexus/archiver/zip/ArchiveFileComparator.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ private static void assertEquals( ArchiveFile file1, TarArchiveEntry entry1,
9494
is1.close();
9595
is2.close();
9696
}
97+
98+
private static void assertEquals( ArchiveFile file1, TarArchiveEntry entry1,
99+
ZipFile file2, ZipArchiveEntry entry2 )
100+
throws IOException
101+
{
102+
Assert.assertEquals( entry1.isDirectory(), entry2.isDirectory() );
103+
104+
final InputStream is1 = file1.getInputStream( entry1 );
105+
final InputStream is2 = file2.getInputStream( entry2 );
106+
final byte[] bytes1 = IOUtil.toByteArray( is1 );
107+
final byte[] bytes2 = IOUtil.toByteArray( is2 );
108+
Assert.assertTrue( Arrays.equals( bytes1, bytes2 ) );
109+
is1.close();
110+
is2.close();
111+
}
112+
97113
private static void assertEquals( ZipFile file1, ZipArchiveEntry entry1,
98114
ZipFile file2, ZipArchiveEntry entry2 )
99115
throws Exception
@@ -136,6 +152,26 @@ public void accept( TarArchiveEntry ze1 )
136152
Assert.assertTrue( map2.isEmpty() );
137153
}
138154

155+
156+
public static void assertEquals( final ArchiveFile file1, final ZipFile file2, final String prefix )
157+
throws Exception
158+
{
159+
final Map<String,ZipArchiveEntry> map2 = getFileEntries( file2 );
160+
forEachTarArchiveEntry( file1, new TarArchiveEntryConsumer()
161+
{
162+
public void accept( TarArchiveEntry ze1 )
163+
throws IOException
164+
{
165+
final String name1 = ze1.getName();
166+
final String name2 = prefix == null ? name1 : ( prefix + name1 );
167+
ZipArchiveEntry ze2 = map2.remove( name2 );
168+
Assert.assertNotNull( ze2 );
169+
assertEquals( file1, ze1, file2, ze2 );
170+
}
171+
} );
172+
Assert.assertTrue( map2.isEmpty() );
173+
}
174+
139175
public static void assertEquals( org.apache.commons.compress.archivers.zip.ZipFile file1, org.apache.commons.compress.archivers.zip.ZipFile file2, String prefix )
140176
throws Exception
141177
{

0 commit comments

Comments
 (0)