下面列出了java.util.jar.JarEntry#getCrc ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public boolean contains( JarFile2 f, JarEntry e )
throws IOException
{
JarEntry thisEntry = getEntryByName( e.getName() );
// Look up name in 'this' Jar2File - if not exist return false
if ( thisEntry == null )
{
return false;
}
// Check CRC - if no match - return false
if ( thisEntry.getCrc() != e.getCrc() )
{
return false;
}
// Check contents - if no match - return false
InputStream oldIS = getJarFile().getInputStream( thisEntry );
InputStream newIS = f.getJarFile().getInputStream( e );
boolean retValue = differs( oldIS, newIS );
return !retValue;
}
public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
try {
if (jf1.size() != jf2.size()) {
throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
+ " have different number of entries");
}
for (JarEntry elem1 : Collections.list(jf1.entries())) {
JarEntry elem2 = jf2.getJarEntry(elem1.getName());
if (elem2 == null) {
throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
}
if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
throw new Exception("The crc of " + elem1.getName() + " is different.");
}
}
} finally {
jf1.close();
jf2.close();
}
}
public String hasSameContent( JarFile2 file, JarEntry entry )
throws IOException
{
String thisName = null;
Long crcL = entry.getCrc();
// check if this jar contains files with the passed in entry's crc
if ( _crcToEntryMap.containsKey( crcL ) )
{
// get the Linked List with files with the crc
LinkedList ll = (LinkedList) _crcToEntryMap.get( crcL );
// go through the list and check for content match
ListIterator li = ll.listIterator( 0 );
while ( li.hasNext() )
{
JarEntry thisEntry = (JarEntry) li.next();
// check for content match
InputStream oldIS = getJarFile().getInputStream( thisEntry );
InputStream newIS = file.getJarFile().getInputStream( entry );
if ( !differs( oldIS, newIS ) )
{
thisName = thisEntry.getName();
return thisName;
}
}
}
return thisName;
}
public void execute() throws BuildException {
try {
JarInputStream jarIn = new JarInputStream( new FileInputStream( archive ) );
JarEntry entry;
while ( ( entry = jarIn.getNextJarEntry() ) != null ) {
String name = entry.getName();
long crc = entry.getCrc();
long size = entry.getSize();
long compressedSize = entry.getCompressedSize();
int compressMethod = entry.getMethod();
long timeStamp = entry.getTime();
int hashCode = entry.hashCode();
StringBuffer out = new StringBuffer();
out.append( "Name: " + name + "\n" );
out.append( " Size: " + Long.toHexString( size ) + " " );
out.append( " Compressed Size: " + Long.toHexString( compressedSize ) + " " );
out.append( " Compression Method: " + compressMethod + " " );
out.append( " Timestamp: " + Long.toHexString( timeStamp ) + " " );
out.append( " HashCode: " + Integer.toHexString( hashCode ) + " " );
out.append( " CRC: " + Long.toHexString( crc ) + " " );
System.out.println( out.toString() );
}
} catch ( Exception e ) {
throw new BuildException( e.getMessage(), e );
}
}