我想尝试使用java.util.zip中的Deflate和Inflate类进行zlib压缩.
我能够使用Deflate压缩代码,但在解压缩时,我遇到了这个错误 -
Exception in thread "main" java.util.zip.DataFormatException: unknown compression method
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:238)
at java.util.zip.Inflater.inflate(Inflater.java:256)
at zlibCompression.main(zlibCompression.java:53)
Run Code Online (Sandbox Code Playgroud)
这是我目前的代码 -
import java.util.zip.*;
import java.io.*;
public class zlibCompression {
/**
* @param args
*/
public static void main(String[] args) throws IOException, DataFormatException {
// TODO Auto-generated method stub
String fname = "book1";
FileReader infile = new FileReader(fname);
BufferedReader in = new BufferedReader(infile);
FileOutputStream out = new FileOutputStream("book1out.dfl");
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(filename));
Deflater compress = new Deflater();
Inflater …Run Code Online (Sandbox Code Playgroud) 我通过动态膨胀来在scrollview中添加一些Linearlayout视图.我已将添加的LinearLayout的背景设置为选择器列表.但是在添加到scrollview后,当我按下所选视图时,它不会显示选择器列表的任何影响.我使用的示例XML是:
选择器文件:selector_file
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/listbg"
android:state_pressed="true" />
<item android:drawable="@drawable/bgsmall"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
我正在膨胀以下视图并添加到l1 Linearlayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_file"
android:gravity="center_vertical"
>
<ImageView android:id="@+id/image1" android:layout_width="100dip" android:layout_height="75dip"/>
<TextView android:id="@+id/textitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000"></TextView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
并且添加了上述膨胀视图的ScrollView是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:id="@+id/l1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
任何的想法...???
我有以下布局.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/list_item_bottom">
<TextView android:id="@+id/list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ViewStub android:id="@+id/stub_for_alt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_item_name"
android:layout="@layout/text_view_alt_name"/>
<ImageView android:id="@+id/list_item_dopinfo1_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/stub_for_alt_name"
android:src="@drawable/ic_released"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我希望ViewStub低于TextView,ImageView低于ViewStub.
ViewStub膨胀的元素按照我的预期显示.但没有ViewStub的元素将TextView与ImageView重叠.
我的布局出了什么问题?
更新:
我发现的唯一解决方案是为ViewStub和相关的TextView提供相同的android:id值.
我有自定义适配器,其中我膨胀布局.在适配器中我尝试在TextView中更改一些文本值(@ id/datetextcal)
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cellView = inflater.inflate(R.layout.calendarcell, null);
TextView dateText = (TextView) cellView.findViewById(R.id.datetextcal);
dateText.setText(position);
Run Code Online (Sandbox Code Playgroud)
我觉得没事,但我有错误!为什么?
没有修改此值,所有编译和工作都正确.
布局代码(R.layout.calendarcell):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:background="@drawable/cell"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="40dp"
android:layout_height="40dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<TextView
android:id="@+id/datetextcal"
android:text="2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:layout_alignRight="@id/datetextcal"
android:paddingRight="2dp"
android:src="@drawable/dropper"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<TextView
android:gravity="center_horizontal"
android:textSize="9dp"
android:text="???????"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
适配器类的代码:
package com.android.wcalendar;
import java.util.Date;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView; …Run Code Online (Sandbox Code Playgroud) 我有一个代码在99%的时间内工作,因为在很多客户端部署,但有时我会得到以下内容:
java.lang.reflect.InvocationTargetException android.widget.LinearLayout.(LinearLayout.java:92)java.lang.reflect.Constructor.constructNative(Native Method)java.lang.reflect.Constructor.newInstance(Constructor.java:446)android .view.LayoutInflater.createView(LayoutInflater.java:499)com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:562)android.view .LayoutInflater.rInflate(LayoutInflater.java:617)android.view.LayoutInflater.inflate(LayoutInflater.java:407)android.view.LayoutInflater.inflate(LayoutInflater.java:320)com.mycode.mycode.MyClass.draw(xxxxxxx ).....
在我的代码上我有:
LayoutInflater li =(LayoutInflater)这个.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
theview = li.inflate(R.layout.partofthescreen,somecontainer,false);
所以问题是为什么我得到InvocationTargetException.
谢谢
Git给了我一个可爱的圣诞礼物......我正在尝试git push一堆提交,比如6 GB.我收到以下错误消息:
-Counting objects: 525, done.
Delta compression using up to 24 threads.
Compressing objects: 100% (474/474), done.
fatal: pack has bad object at offset 3453162391: inflate returned -5
error: pack-objects died of signal 13
error: failed to push some refs to ....git
Run Code Online (Sandbox Code Playgroud)
这是什么意思,它是如何修复的?从谷歌我可以看出它与我试图推动的大小有关.
这是在写作时发生的.
我有一些关于膨胀和重新使用相同TextView的问题.
它就像试图一遍又一遍地覆盖相同的文本视图一样,它不能这样做?
LayoutInflater inflater = LayoutInflater.from(this);
View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
View layout_number = inflater.inflate(R.layout.inflate_number, null);
for (int i = 0; i < 10; i++) {
row = new TableRow(this);
number = (TextView) layout_number.findViewById(R.id.Number);
number.setTag(i);
number.setText(Integer.toString(i));
row.addView(number);
}
setContentView(mainlayout);
Run Code Online (Sandbox Code Playgroud)
这是inflate_number.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Number"
android:layout_width="3dp"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:text="1" />
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误和它的行:51,这是:
row.addView(number);
07-18 20:54:25.124: E/AndroidRuntime(1166): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 07-18 20:54:25.124: E/AndroidRuntime(1166): at android.view.ViewGroup.addViewInner(ViewGroup.java:1970) 07-18 20:54:25.124: E/AndroidRuntime(1166): …
这是我的代码,我是 Go 的新手。我试着用谷歌搜索这个问题,但我不能完全解决它。我认为这与Read()方法有关。
package main
import (
...
)
type compressor struct {
content []byte
}
func (r *compressor) compress() []byte {
...
}
func (r *compressor) decompress() []byte {
var buffer bytes.Buffer
dc := flate.NewReader(&buffer)
_, err := dc.Read(r.content)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
}
return buffer.Bytes()
}
func main() {
fileName := os.Args[1]
fmt.Println(os.Args)
contents, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal(err)
}
fmt.Print("Uncompressed data: ")
fmt.Println(len(contents)) …Run Code Online (Sandbox Code Playgroud) 我是过时的帝国时代II(AoE)游戏的粉丝.我想用Python编写AoE游戏记录(.mgx文件)的解析器.
我在GitHub上做了一些搜索并发现了很少的项目,最有用的是aoc-mgx格式,它提供了.mgx游戏记录文件的一些细节.
这是问题所在:
根据参考文献,.mgx文件的结构如下:
| header_len(4byte int)| next_pos(4byte int)| header_data | ...... |
十六进制数据的mgx格式的字节顺序是小端.
header_len存储Header部分的数据长度(header_len + next_post + header_data)
header_data存储我需要的有用信息,但它用zlib压缩
我试图用zlib模块解压缩header_data中的数据,如下所示:
import struct
import zlib
with open('test.mgx', "rb") as fp:
# read the header_len bytes and covert it to a int reprents length of Header part
header_len = struct.unpack("<i", fp.read(4))[0]
# read next_pos (this is not important for me)
next_pos = struct.unpack("<i", fp.read(4))[0]
# then I …Run Code Online (Sandbox Code Playgroud) 我正在通过nodejs将java应用程序中的压缩数据发送到网页.使用java deflater和base64编码压缩数据.在网页上我试图用https://github.com/dankogai/js-deflate来扩充数据,但它不起作用(空结果).我错过了什么吗?
Java方面:
private String compress(String s) {
DeflaterOutputStream def = null;
String compressed = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// create deflater without header
def = new DeflaterOutputStream(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
def.write(s.getBytes());
def.close();
compressed = Base64.encodeBase64String(out.toByteArray());
System.out.println(compressed);
} catch(Exception e) {
Log.c(TAG, "could not compress data: " + e);
}
return compressed;
}
Run Code Online (Sandbox Code Playgroud)
Javascript方面:
var data = RawDeflate.inflate(Base64.fromBase64(compressed));
Run Code Online (Sandbox Code Playgroud) inflate ×10
android ×5
zlib ×3
deflate ×2
java ×2
binaryfiles ×1
compression ×1
eof ×1
git ×1
go ×1
javascript ×1
python ×1
selector ×1
viewstub ×1
xml ×1